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

Marcin Szamotulski mszamot at gmail.com
Mon Jun 17 23:22:07 EDT 2013


> While you said to me to forget about memory locations, and that's indeed 
> made things easy to follow i still keep wondering, how Python internally 
> keeping tracks of 'x' and 'y' names as well as their referenced objects 
> (i.e. number 6).

There is an excellent blog post about CPython internals:
http://tech.blog.aknin.name/category/my-projects/pythons-innards/
but it might be difficult to follow if you cannot at least read C.
It explains how python virtual machine works internally, how the opcodes
are evaluated, how the three scopes globals, locals and builins find its
place (and how names are bind).  As far as I understand names are keys
in the scope dictionaries, which are exposed in the python level as
locals(), globals() and the builtin's.  This is how Python tracks names
and their values.  To be more precise, when you do:

>>> a = 1
>>> def f():
...  b=2

in the first line 'a' is added to the globals() dictionary with value 1,
and in the third line 'b' with value 2 is added to the local dictionary
of f.  It is also all explained in the python docs, and it reflects how
python is implemented (at least CPython).

> 
> After all the way i understand memory is as a series of bits like:
> 
> 0100010100011110101000010101010010001001010010011100001101001010010
> 
> So from the above binary form:
> 
> what is 'x', what is 'y', how's 'x' and 'y' differ from the actually 
> memory locations that are bound too, and of course what is the actual value.

'x' and 'y' are just strings which are written somewhere in the computer's
memory.

> 
> Its 3 things for me to consider, even in Python id internal level 
> detail. I want to understand this.
> 
> names, memory addresses, memory address's actual values

Names and values are not connected through their memory addresses but
because they live in a higher structure, name is a key and value is a value
of a dictionary (which is also represented in some way at the C level,
namely by PyDictObject ... but for this you need to first learn C, but
please read and understand the Python docs - there is already a lot
there for you ...

Best regards,
Marcin



More information about the Python-list mailing list