This is the python function from my pluralsight python class:
def raise_to(exp):
def raise_to_exp(x):
return pow(x, exp)
return raise_to_exp
And the instructor now opens an interactive session and does the following:
- from raise_to import raise_to
- square = raise_to(2), and then goes on to do
- square(5)
and that produces the result of 25. How or why pass in two distinct arguments? Now I ran a debug on this code and this is what I observed. When I do this:
def raise_to(exp):
def raise_to_exp(x):
return pow(x, exp)
return raise_to_exp
square = raise_to(2)
print(square)
I get : <function raise_to.<locals>.raise_to_exp at 0x00000246D1A88700>
, but if I do as the instructor
def raise_to(exp):
def raise_to_exp(x):
return pow(x, exp)
return raise_to_exp
square = raise_to(2)
print(square(5))
I get 25. I would like to know how this works. I know this is referred as a python factory function but how does it work. Is the function storing the first argument for later use with the second argument passed in?