Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jun 17 19:09:08 EDT 2013


On Mon, 17 Jun 2013 14:34:57 +0300, Simpleton wrote:

> On 17/6/2013 9:51 πμ, Steven D'Aprano wrote:
>> Now, in languages like Python, Ruby, Java, and many others, there is no
>> table of memory addresses. Instead, there is a namespace, which is an
>> association between some name and some value:
>>
>> global namespace:
>>      x --> 23
>>      y --> "hello world"
> 
> First of all thanks for the excellent and detailed explanation Steven.
> 
> As for namespace:
> 
> a = 5
> 
> 1. a is associated to some memory location 

No. a is associated to an object, the int 5, which may be free to move in 
memory (PyPy does this), or may be at a fixed memory location (CPython 
does this). But the association is with the object, not the memory 
location.

The object is a data structure that contains a type (int), a value (5), 
and various methods (e.g. bit_length, to_bytes).


2. the latter holds value 5
> 
> So 'a', is a reference to that memory location, so its more like a name
> to that memory location, yes? Instead of accessing a memory address with
> a use of an integer like "14858485995" we use 'a' instead.

No. We're talking about what the Python interpreter does, not what you 
do. Whether you are programming in C or Python, you still refer to 
variables by name:

a = 5

but what goes on inside the interpreter or compiler is different.


> So is it safe to say that in Python a == &a ? (& stands for memory
> address)

Absolutely not.


> is the above correct?
> 
> I say this because here you said that: Instead, there is a namespace,
> which is anassociation between some name and some value:
> 
> When you say that you mean that a is associated to some value as in
> memory location or to that memory location's address?

No. This has nothing to do with memory locations. Namespaces are dicts. 
Write down a dict:

{"a": "Hello world"}

Do you see a memory location there? There is no memory location. There is 
the name, "a", and the object it is associated with, "Hello world". 
Either the dict, or the string, may move around memory if the underlying 
memory manager allows it. Obviously any object in Python must be 
*somewhere* in memory at any specific moment, but that is irrelevant to 
understanding Python code. It is no more relevant than saying that a dict 
{'a': 5} is just made up of bits.



-- 
Steven



More information about the Python-list mailing list