I'm trying to declare two functions within exec statement in python. Let's call them f1() and f2().
I've found out that when exec is invoked inside of some function, then f2() has no visibility of f1(). However this doesn't happen when exec and function call are placed in global code.
# Case 1: Working fine
code = """
def f1(): print "bar"
def f2(): f1()
"""
exec(code)
f2() # Prints "bar" as expected
# Case 2: Throws NameError: global name 'f1' is not defined
code = """
def f1(): print "bar"
def f2(): f1()
"""
def foo():
exec(code)
f2() # NameError
foo()
Can someone explain me how to avoid that NameError and make exec work inside of a function?