Results running the code for python3:
------- input option 1 - exec code is executed --------
0 - for running inside function, 1 - for running in main programa: 1
option = 1
10
------- input option 0 - exec code not executed inside function ----
0 - for running inside function, 1 - for running in main programa: 0
option = 0
code inside execfunction => A = 10
Traceback (most recent call last):
File "myexec.py", line 19, in <module>
print(A)
NameError: name 'A' is not defined
---------------------Code --------------------------
myexec.py
def execfunction(icode):
print('code inside execfunction => %s' %(icode))
exec(icode)
option = int(input("0 - for running inside function, 1 - for running in main programa: "))
print('option = %d' %(option))
code = 'A = 10'
if (option == 1):
exec(code)
else:
execfunction(code)
print(A)