Global Namespace

The global namespace contains any names defined at the position of the main program. Namespace in Python creates the global namespace when the main program body starts, and it remains in actuality until the practitioner terminates.Rigorously speaking, this may not be the only global namespace that exists. The practitioner also creates a global namespace for any module that your program loads with the import statement.




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>

Comments

Popular posts from this blog

Unleashing Creativity: The Latest Frontier of Animation AI

Tanh Activation Function

Unveiling the Hidden Gems: Exploring Data Mining Functionality