I am just curious, I just recently learned that the Python built-in, global, and local namespace that the interpreter uses to reference objects is basically a Python dictionary.
I am curious as to why when the global() function is called, the dictionary prints the variable objects in a string but the objects in functions defined refer to a hardware memory address?
For example-
This script:
print("Inital global namespace:")
print(globals())
my_var = "This is a variable."
print("Updated global namespace:")
print(globals())
def my_func():
my_var = "Is this scope nested?"
print("Updated global namespace:")
print(globals())
Outputs this:
Inital global namespace:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a483208>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'global_check', '__cached__': None}
Updated global namespace:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a483208>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'global_check', '__cached__': None, 'my_var': 'This is a variable.'}
Updated global namespace:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10a483208>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'global_check', '__cached__': None, 'my_var': 'This is a variable.', 'my_func': <function my_func at 0x10a40ce18>}
'my_var': 'This is a variable.', 'my_func': <function my_func at 0x10a40ce18>
I understand some people might think these kind of things aren't important, but if I wanted to see the object for a function would I even be able to do such a thing? Does this question make sense?
globals()only works for the global scope. Everything in classes and functions is local scope. There is no way from outside this function to access its local variables because they are not even initialized before calling the function.<function my_func at 0x10a40ce18>in the output, I have to ask: How else should it print a function?my_var = "Is this scope nested?"