source_code = '''
def fn():
print('source code executed')
return 100
exec_val = fn()
'''
class Executor:
@classmethod
def run(cls, code):
exec_val = 0
exec(code)
return exec_val
if __name__ == '__main__':
print(Executor.run(source_code))
>>> source code executed
>>> 0
The result of code above is 0
which is not what I expected 100
.
I've tried add global exec_val
statement both in source_code
and Executor.run
, but still not work.
Python 3.6.8