The following code throws an exception:
import inspect
def work():
my_function_code = """def print_hello():
print('Hi!')
"""
exec(my_function_code, globals())
inspect.getsource(print_hello)
The code above throws an exception IOError. If I declare the function without using exec (like below), I can get its source code just fine.
import inspect
def work():
def print_hello():
print('Hi!')
inspect.getsource(print_hello)
There's a good reason for me to do something like this.
Is there a workaround for this? Is it possible to do something like this? If not, why?