Assume a nested function in python, where outer
denotes the outer function and inner
denotes the inner function. The outer function provides parametrization of the inner function and returns an instance of this parametrized function.
I want to obtain the name of the outer function, given an instance of the inner function which is returned by the outer function. The code is as follows:
def outer(a):
def inner(x):
return x*a
return inner
p = outer(3)
print p # prints inner
print p(3) # prints 9
How can I print the name of the outer function, ie outer
, given only p
?
Thanks!
You can use functools.wraps
:
from functools import wraps
def outer(a):
@wraps(outer)
def inner(x):
return x*a
return inner
p = outer(3)
print p # <function outer at ...>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With