This question already has answers here:
Closed 2 years ago.
I have defined a function within another function in python and now I would like to call the inner function. Is this possible, in python?
How do I call func2
from func3
?
def func1():
def func2():
print("Hello!")
def func3():
# Enter code to call func2 here
You can't, at least not directly.
I'm not sure why you would want to do that. If you want to be able to call func2()
from outside func1()
, simply define func2()
at an appropriate outer scope.
One way that you could do it is to pass a parameter to func1()
indicating that it should invoke func2()
:
def func1(call_func2=False):
def func2():
print("Hello!")
if call_func2:
return func2()
def func3():
func1(True)
but since that requires modification to the existing code, you might as well move func2()
to the same scope as func1()
.
I don't recommend that you do this, however, with some indirection you can reach into the func1()
function object and access it's code object. Then using that code object access the code object for the inner function func2()
. Finally call it with exec()
:
>>> exec(func1.__code__.co_consts[1])
Hello!
To generalise, if you had multiple nested functions in an arbitrary order and you wanted to call a specific one by name:
from types import CodeType
for obj in func1.__code__.co_consts:
if isinstance(obj, CodeType) and obj.co_name == 'func2':
exec(obj)
Let's go into little deep and explore it :
You can do this via three methods :
First method:
Just return the nested function from main function so return will become the caller of nested function:
def func1():
def func2():
print("Hello!")
return func2()
def func3():
return func1()
func3()
output:
Hello!
Second method : Even you can pass argument directly to nested
function:
Using Closure concept :
def func1():
def func2(x):
print("Hello {}".format(x))
return func2
closure=func1()
def func3():
return closure('bob')
func3()
Look above example , Main function is not accepting any parameter but nested function have one parameter so here i directly pass the argument to nested function.
Third method :
You can try this little hacky thing :
def func1():
def func2():
print("Hello")
return func2
def func3():
return func1()()
func3()