I was thinking about some python code that I had recently written, and since I like to think of how the libraries code out their capabilities, I wanted to figure out a bit of code that takes a function and runs it.
That function probably was layed out like this:
def run_func(func):
func()
but it also takes an input for the normal inputs that you would give it. So let’s say you have this function, add(x, y)
but as you can see, it takes two inputs. So the way you would run that function like this:
run_func(add, [3, 2])
meaning the definition of the function could be this:
def run_func(func, inputs = []):
...
I got to this trying to figure out the mystery:
def run_func(func, inputs = []):
for x in range(len(inputs)):
variable{{x}} = inputs[x] # means that the variable is variable followed with the value of x
num_inputs = len(inputs)
func(inputs[0], ..., inputs[num_inputs])
shourya bajjuri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.