When run in a script, the x
, y
, and z
below all point to the same dict
object.
test_globals.py
import sys
x = globals()
y = sys.modules["__main__"].__dict__
def foo():
print("foo.")
z = foo.__globals__
To check, run python -i test_globals.py
and run the following in the interpreter.
python
>>> x is y is z
True
>>> [id(w) for w in (x, y, z)]
[10317376, 10317376, 10317376]
Recommended Posts