2

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?

6
  • 2
    Functions are stored as references in memory. There are different scopes in python and 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. Commented Apr 17, 2018 at 15:05
  • 2
    "prints the variable objects in a string but the objects in functions defined refer to a hardware memory address" I don't understand this sentence. It prints the name and value of each variable. If you're surprised to see <function my_func at 0x10a40ce18> in the output, I have to ask: How else should it print a function? Commented Apr 17, 2018 at 15:05
  • @Aran-Fey I'm surprised it doesn't print something like my_var = "Is this scope nested?" Commented Apr 17, 2018 at 15:07
  • 1
    Because that's a local variable. It doesn't exist in the global scope. Commented Apr 17, 2018 at 15:08
  • Ah, okay that makes sense Aran-Fey. @Jonathan R Maybe I'm still missing something but is there any reason as to WHY the function is referenced as a hardware address and a variable is referenced as a string? Commented Apr 17, 2018 at 15:11

2 Answers 2

2

When you type my_var in your code, Python needs to know what that represents. First it checks in locals and if it doesn't find a reference it checks in globals. Now Python can evaluate your code when you write 'string' == my_var. Similarly, when you write the name of a function in your code, Python needs to know what that represents. Since the output of a function can change based on the input, Python can't store a simple value like a string to represent a function in globals. Instead it stores the memory reference so that when you type my_func(), it can go to that memory store and use the function to compute the output.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh! So it's because output for a function can change based on input, that's exactly what I need to know thank you @bphi.
0

use global my_var Then you can assign your global variables in local namespaces

2 Comments

Thank you Null, maybe my question wasn't clear enough. I am wondering why functions have a hardware address reference in the global namespace but variables are printed in a string?
in python, functions are also variables and globals() returns the dict of all global variables with names as keys

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.