For example:
def foo():
def bar():
return
# some code
return
def foo2():
# call bar() here?
Is it possible to put bar()
inside foo()
and call it in foo2()
?
For example:
def foo():
def bar():
return
# some code
return
def foo2():
# call bar() here?
Is it possible to put bar()
inside foo()
and call it in foo2()
?
Somewhat, you can do that - by declaring bar
global:
def foo():
global bar
def bar():
print("In bar")
But you cannot call bar
unless you first call foo
, because that's the function that defines bar
:
bar()
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#NameError: name 'bar' is not defined
foo()
bar()
#In bar
Overall, this is a very bad idea.