From a comment: "global in Python basically means at the module-level". However running this code in a file named my_module.py:
import my_module as m
foo = 1
m.bar = m.foo + 1
if __name__ == "__main__":
print('foo:', foo)
print('m.foo:', m.foo)
print('m.bar:', m.bar, '\n')
for attrib in ('foo', 'bar'):
print("'{0}' in m.__dict__: {1}, '{0}' in globals(): {2}".format(
attrib,
attrib in m.__dict__,
attrib in globals()))
Output:
foo: 1
m.foo: 1
m.bar: 2
'foo' in m.__dict__: True, 'foo' in globals(): True
'bar' in m.__dict__: True, 'bar' in globals(): False
What exactly are the module and global namespaces?
Why is there a __dict__ attribute in module namespace but not in global namespace?
Why is m.bar part of __dict__ and not part of globals()?



