<p>I am confused about the implementation of a global namespace in python .
How are variable names mapped as keys to the objects they reference as values ,since namespace is implemented as a dictionary? If keys of a typical python dict are “objects” themselves, why are variables called keys if they are just references not the actual objects?</p>

<syntax><language-design><implementation><parsing><python>
How is a python namespace implemented in terms of memory under Cpython implementation?
CC BY-SA 4.0
2024-07-07T07:03:47.443Z
I don’t quite get what you are asking. Are you confused how variables (as source strings, runtime objects, …?) are treated as keys for the global dict? Are you confused how, generally, objects are treated as keys of dicts given reference semantics? Or something else entirely? Do you already understand how string objects are used as keys in dict objects?
MisterMiyagi
CC BY-SA 4.0
2024-07-07T07:30:42.600Z
Please correct me if I am wrong . I understand that variables are names that point to objects ( string, int, float…) but they are added to the global namespace  which is under the hood implemented as a dictionary . Moreover ,what I know is that a typical python dictionary has (keys that can be any immutable  and hashable object ) but the variables as names in the namespace are considered to be keys. What are they actually , are they just holding the address of object (values ). Please explain this Example: x=5 in terms of its implementation as in a namespace ( dictionary if it is the case)
Silah
CC BY-SA 4.0
2024-07-07T07:56:12.153Z
You can access the global dictionary itself with `globals()` to inspect it and see how it (apparently) behaves. I also don't understand what you're asking — the question seems to answer itself when it says that the variable names are mapped as keys to the objects they reference as values, but clearly that *isn't* what you want to know! You can [edit] your question.
Michael Homer
CC BY-SA 4.0
2024-07-07T08:13:55.143Z
Umm I am not being able to clearly ask . Let me put it in a better way . I am asking what is the difference between an internal implementation of a dictionary and a global namespace. Are the variable names treated as keys ? I ‘ll give an example: dic={“citrus “: “orange”}  . 
citrus=“orange” . Now citrus here is a variable. How is the string “citrus” implementation in memory  as a key different from variable citrus as a key in namespace?
Silah
CC BY-SA 4.0
2024-07-07T08:20:58.350Z
I'm guessing your confusion is actually with variables, not the use of globals and their keys. It seems like you are stuck on a sort of use-mention distinction between the variable as something used by Python and the variable as something represented by Python, with the challenge of separating the two without infinitely recursing between the concepts. Do you understand how locals work, i.e. how a local variable maps to locals or closures?
MisterMiyagi
CC BY-SA 4.0
2024-07-07T08:34:35.893Z
I guess I need clarification on the concept of both , how python uses and represents variables ? 
I hope this helps me understand further concepts. 
Actually I am a beginner in Python , not so acquainted with the terms and concepts. 
If you could refer me to links to resources, that would be a great help
Silah
<p>Python allows you to inspect the global scope with the <code>globals()</code> function. Let's try it out:</p>
<pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; globals()
{'__name__': '__main__', '__doc__': None, '__package__': None,
'__loader__': &lt;class '_frozen_importlib.BuiltinImporter'&gt;,
'__spec__': None, '__annotations__': {},
'__builtins__': &lt;module 'builtins' (built-in)&gt;}
</code></pre>
<p>We can create a new variable in scope:</p>
<pre class="lang-python prettyprint-override"><code>x = 5
</code></pre>
<p>and then look at the globals dictionary again:</p>
<pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; globals()
{'__name__': '__main__', '__doc__': None, '__package__': None,
'__loader__': &lt;class '_frozen_importlib.BuiltinImporter'&gt;,
'__spec__': None, '__annotations__': {},
'__builtins__': &lt;module 'builtins' (built-in)&gt;, 'x': 5}
</code></pre>
<p>We can see that there is a new key, <code>x</code>, with the name of the variable we created and the value I assigned to it. It's the <em>name</em> of the variable, as a string, that is the key here.</p>
<p>We can use that string name as a key directly if we like:</p>
<pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; globals()[&quot;x&quot;] = &quot;hello&quot;
&gt;&gt;&gt; x
'hello'
</code></pre>
<p>You can see that the value of the <code>x</code> variable has changed when I updated the value in the <code>globals()</code> dictionary at the key <code>&quot;x&quot;</code>. The language runtime understood to take that variable name and get it from the dictionary, and vice-versa. In this way, the global variables and the dictionary entries really are identified with one another.</p>
<p>This is a reasonable enough way of implementing variable scopes in a dynamically-typed language: you can use dictionary/map/table lookup semantics that you already have to drive your variables. JavaScript's global scopes work the same way, for example. The language, whichever one it is, just <strong>turns a read or write of a variable to an access to the scope dictionary at the key determined by the variable's <em>name</em></strong>.</p>
<p>Python also exposes local variables the same way (using <code>locals()</code>), but JavaScript doesn't. You can imagine that the interpreter always holds on to two references to dictionaries, one for global variables and one for locals, and looks up any given variable in the corresponding dictionary. This is something that the language <em>implementation</em> knows to do — it's not happening &quot;within&quot; the user-level program, so you don't have to write <code>globals[&quot;x&quot;] = 5</code> every time you want to make an assignment, you just write the variable name and it does the rest internally.</p>
<p>Lots of small programming language implementations follow exactly this strategy for their variables: they give each scope a dictionary (map, table, hash, ...) for all the variables in it and turn every variable access into dictionary access, and that's it. This is convenient for implementation, because you can just make a data structure from the standard library and use it the way it's meant to be used, including for creating predefined scopes, printing it out for debugging, or lots of other common tasks.</p>
<hr />
<p>There are a lot of fine implementation details behind the scenes here. A language doesn't need to use the same implementation strategy internally as it exposes to the user, as long as the observable behaviour is the same. High-performance JavaScript implementations generally try to optimise away these kinds of lookup, for example.</p>
<p>For the level of understanding you're looking for, we can treat any access to a variable as translating to a lookup in a specific dictionary using the variable's name, as a string, as the key. The implementation inherently knows where to find that dictionary for the current scope, and it knows it has to treat a variable that way when it sees one.</p>
<p>How string lookup inside a dictionary works is also an implementation detail. You could imagine that as a big list of pairs <code>(key, value)</code> and a for loop that does a linear search through the whole list to find a matching key and return the value that came with it. This has the right semantics, but it's slower than ideal, and real implementations will likely use a more efficient data structure and lookup algorithm. It doesn't matter whether the key came from a variable name, a string literal, or user input, the dictionary will work the same way regardless.</p>

CC BY-SA 4.0
2024-07-07T08:52:51.290Z
2024-07-07T08:52:51.290Z
Michael Homer
CC BY-SA 4.0
2024-07-07T09:06:44.040Z
I want to thank you for such clear explanation. This helped  me revisit my concept . I understood. Actually with so many resources all over the internet, plus considering one is a beginner, it is really hard to grasp the basics technically and then moving on to advanced topics
Silah
CC BY-SA 4.0
2024-07-07T09:12:45.950Z
Are there good resources online that can help me gain and strengthen my understanding about the topics relevant to how programming languages work internally ? Also , do I really need to know about this in depth  to write efficient programs or does it just depend on whether I am opting to learn about the  workings of interpreted languages’ implementation?
Silah
CC BY-SA 4.0
2024-07-07T09:28:07.423Z
I don't think you need to understand the internals of the implementations at this point. Those can get very complicated. For any real-world efficiency question, the contribution of the language is something you determine by profiling rather than inspection, and in most cases the program body will dominate anyway (and in any case, concerns about efficiency are probably premature optimisation right now). Most users of programming languages don't have in-depth knowledge of the implementation details of the language they are using.
Michael Homer
CC BY-SA 4.0
2024-07-07T09:37:55.727Z
Thanks once again!
Silah
CC BY-SA 4.0
2024-07-07T11:51:54.430Z
FWIW, in (common implementations of) Python locals are just represented as a dict of the user requests it, but not actually stored as such. In CPython, locals are an array of pointers, with variable names compiled to indices into that array.
MisterMiyagi
CC BY-SA 4.0
2024-07-07T17:32:33.790Z
Thanks . This adds to my understanding. Further clarified the concept .I kind of tend to get stuck in something that I find confusing, knowing this is a bad approach, even then I cannot help it . 
To get guidance and streamline my approach towards learning  is the reason I turned to this community . Looking forward to participating.
Silah
CC BY-SA 4.0
2024-07-07T19:49:07.697Z
"*implementing variable scopes in a dynamically-typed language*" - not sure what this has to do with types, couldn't this be done for every language? Is it that dictionaries can hold heterogeneous values? Or that they don't need to store type information for each key/variable? At first I though you would refer to languages where *scopes* are dynamic (i.e. mutable, one can introduce new variables at runtime).
Bergi
CC BY-SA 4.0
2024-07-07T21:29:11.260Z
@Bergi *Is it that dictionaries can hold heterogeneous values?* Yes. Of course you can do it elsewhere too, with a little more work.
Michael Homer
CC BY-SA 4.0
2024-07-09T21:01:16.313Z
The consequence of what @MisterMiyagi says is that you can't assign to `locals()` similarly to the `globals()` assignment in the answer. `locals()["foo"] = "bar"` will not create a local variable named `foo`.
Barmar
# How is a python namespace implemented in terms of memory under Cpython implementation?
Posted by: username_0 on 2024-07-07
I am confused about the implementation of a global namespace in python .
How are variable names mapped as keys to the objects they reference as values ,since namespace is implemented as a dictionary? If keys of a typical python dict are “objects” themselves, why are variables called keys if they are just references not the actual objects?
**Tags:** syntax language-design implementation parsing python
> **Comments:**
> **username_1**: I don’t quite get what you are asking. Are you confused how variables (as source strings, runtime objects, …?) are treated as keys for the global dict? Are you confused how, generally, objects are treated as keys of dicts given reference semantics? Or something else entirely? Do you already understand how string objects are used as keys in dict objects?  
> **username_0**: Please correct me if I am wrong . I understand that variables are names that point to objects ( string, int, float…) but they are added to the global namespace  which is under the hood implemented as a dictionary . Moreover ,what I know is that a typical python dictionary has (keys that can be any immutable  and hashable object ) but the variables as names in the namespace are considered to be keys. What are they actually , are they just holding the address of object (values ). Please explain this Example: x=5 in terms of its implementation as in a namespace ( dictionary if it is the case)  
> **username_2**: You can access the global dictionary itself with `globals()` to inspect it and see how it (apparently) behaves. I also don't understand what you're asking — the question seems to answer itself when it says that the variable names are mapped as keys to the objects they reference as values, but clearly that *isn't* what you want to know! You can [edit] your question.  
> **username_0**: Umm I am not being able to clearly ask . Let me put it in a better way . I am asking what is the difference between an internal implementation of a dictionary and a global namespace. Are the variable names treated as keys ? I ‘ll give an example: dic={“citrus “: “orange”}  . 
citrus=“orange” . Now citrus here is a variable. How is the string “citrus” implementation in memory  as a key different from variable citrus as a key in namespace?  
> **username_1**: I'm guessing your confusion is actually with variables, not the use of globals and their keys. It seems like you are stuck on a sort of use-mention distinction between the variable as something used by Python and the variable as something represented by Python, with the challenge of separating the two without infinitely recursing between the concepts. Do you understand how locals work, i.e. how a local variable maps to locals or closures?  
> **username_0**: I guess I need clarification on the concept of both , how python uses and represents variables ? 
I hope this helps me understand further concepts. 
Actually I am a beginner in Python , not so acquainted with the terms and concepts. 
If you could refer me to links to resources, that would be a great help  
---
## 1 answers
### Answer by username_2 on 2024-07-07. Score: 3 | Accepted answer
Python allows you to inspect the global scope with the globals() function. Let's try it out:
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None,
'__loader__': ,
'__spec__': None, '__annotations__': {},
'__builtins__': }

We can create a new variable in scope:
x = 5

and then look at the globals dictionary again:
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None,
'__loader__': ,
'__spec__': None, '__annotations__': {},
'__builtins__': , 'x': 5}

We can see that there is a new key, x, with the name of the variable we created and the value I assigned to it. It's the name of the variable, as a string, that is the key here.
We can use that string name as a key directly if we like:
>>> globals()["x"] = "hello"
>>> x
'hello'

You can see that the value of the x variable has changed when I updated the value in the globals() dictionary at the key "x". The language runtime understood to take that variable name and get it from the dictionary, and vice-versa. In this way, the global variables and the dictionary entries really are identified with one another.
This is a reasonable enough way of implementing variable scopes in a dynamically-typed language: you can use dictionary/map/table lookup semantics that you already have to drive your variables. JavaScript's global scopes work the same way, for example. The language, whichever one it is, just turns a read or write of a variable to an access to the scope dictionary at the key determined by the variable's name.
Python also exposes local variables the same way (using locals()), but JavaScript doesn't. You can imagine that the interpreter always holds on to two references to dictionaries, one for global variables and one for locals, and looks up any given variable in the corresponding dictionary. This is something that the language implementation knows to do — it's not happening "within" the user-level program, so you don't have to write globals["x"] = 5 every time you want to make an assignment, you just write the variable name and it does the rest internally.
Lots of small programming language implementations follow exactly this strategy for their variables: they give each scope a dictionary (map, table, hash, ...) for all the variables in it and turn every variable access into dictionary access, and that's it. This is convenient for implementation, because you can just make a data structure from the standard library and use it the way it's meant to be used, including for creating predefined scopes, printing it out for debugging, or lots of other common tasks.

There are a lot of fine implementation details behind the scenes here. A language doesn't need to use the same implementation strategy internally as it exposes to the user, as long as the observable behaviour is the same. High-performance JavaScript implementations generally try to optimise away these kinds of lookup, for example.
For the level of understanding you're looking for, we can treat any access to a variable as translating to a lookup in a specific dictionary using the variable's name, as a string, as the key. The implementation inherently knows where to find that dictionary for the current scope, and it knows it has to treat a variable that way when it sees one.
How string lookup inside a dictionary works is also an implementation detail. You could imagine that as a big list of pairs (key, value) and a for loop that does a linear search through the whole list to find a matching key and return the value that came with it. This has the right semantics, but it's slower than ideal, and real implementations will likely use a more efficient data structure and lookup algorithm. It doesn't matter whether the key came from a variable name, a string literal, or user input, the dictionary will work the same way regardless.
> **Comments:**
> **username_0** (2024-07-07): I want to thank you for such clear explanation. This helped  me revisit my concept . I understood. Actually with so many resources all over the internet, plus considering one is a beginner, it is really hard to grasp the basics technically and then moving on to advanced topics  
> **username_0** (2024-07-07): Are there good resources online that can help me gain and strengthen my understanding about the topics relevant to how programming languages work internally ? Also , do I really need to know about this in depth  to write efficient programs or does it just depend on whether I am opting to learn about the  workings of interpreted languages’ implementation?  
> **username_2** (2024-07-07): I don't think you need to understand the internals of the implementations at this point. Those can get very complicated. For any real-world efficiency question, the contribution of the language is something you determine by profiling rather than inspection, and in most cases the program body will dominate anyway (and in any case, concerns about efficiency are probably premature optimisation right now). Most users of programming languages don't have in-depth knowledge of the implementation details of the language they are using.  
> **username_0** (2024-07-07): Thanks once again!  
> **username_1** (2024-07-07): FWIW, in (common implementations of) Python locals are just represented as a dict of the user requests it, but not actually stored as such. In CPython, locals are an array of pointers, with variable names compiled to indices into that array.  
> **username_0** (2024-07-07): Thanks . This adds to my understanding. Further clarified the concept .I kind of tend to get stuck in something that I find confusing, knowing this is a bad approach, even then I cannot help it . 
To get guidance and streamline my approach towards learning  is the reason I turned to this community . Looking forward to participating.  
> **username_3** (2024-07-07): "*implementing variable scopes in a dynamically-typed language*" - not sure what this has to do with types, couldn't this be done for every language? Is it that dictionaries can hold heterogeneous values? Or that they don't need to store type information for each key/variable? At first I though you would refer to languages where *scopes* are dynamic (i.e. mutable, one can introduce new variables at runtime).  
> **username_2** (2024-07-07): @Bergi *Is it that dictionaries can hold heterogeneous values?* Yes. Of course you can do it elsewhere too, with a little more work.  
> **username_4** (2024-07-09): The consequence of what @MisterMiyagi says is that you can't assign to `locals()` similarly to the `globals()` assignment in the answer. `locals()["foo"] = "bar"` will not create a local variable named `foo`.  
---
