I am using Python 3.11.9.
Explain why there is no result
variable in the second output.
def foo():
exec('a = 10; b = 5; result = a * b')
return locals()
def bar():
exec('a = 10; b = 5; result = a * b')
return locals()
result = 100
print(foo()) # {'a': 10, 'b': 5, 'result': 50}
print(bar()) # {'a': 10, 'b': 5}
I expected the same result as after executing the first function.
New contributor
Daniil Ivanov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.