|
|
|
|
|
by sabauma
3301 days ago
|
|
The REPL essentially operates at the global scope, which is represented as a dictionary. Variables local to a function are not stored in a dictionary, however: def main():
a = 1
d = locals()
print d
d['b'] = 123
print b
print d
print d is locals()
main()
Which prints {'a': 1}
Traceback (most recent call last):
File "test.py", line 23, in <module>
main()
File "test.py", line 19, in main
print b
NameError: global name 'b' is not defined
|
|
To show that not even changes to existing local variables work, try
which prints I.e. changes to the dictionary returned by locals() are ignored, and calling locals() again overrides the previous values.