Python internals

Larry Bates larry.bates at websafe.com`
Tue Jul 15 10:29:58 EDT 2008


Peter Anderson wrote:
> Hi!  I am slowly teaching myself Python.  I was reading David Beazley's 
> excellent book "Python - Essential Reference"; in particular about 
> variables.  Let me quote:
> 
> "Python is a dynamically typed language in which names can represent 
> values of different types during the execution of a program. In fact the 
> names used in the program are really just labels for various quantities 
> and objects. The assignment operator simply creates an association 
> between a name and a value. This is different from C, for example, in 
> which a name (variable) represents a fixed size and location in memory..."
> 
> As an old mainframe programmer, I understand the way C does things with 
> variable but this text got me wondering how Python handles this 
> "association" between variable name and value at the lower level.  Is it 
> like a fifo list?
> 
> If there is any Python guru that can help I would be most interested in 
> your thoughts.
> 
> Regards,
> Peter

Names are pointers in Python that point to values in memory.  Names are "bound" 
to these values with assignment.  Names are NOT buckets where you put values as 
is the case (or thought process) in other languages.  Example:

a = list(1,2,3)
b = a

Now a AND b point to the same list in memory

Note: if you wanted a COPY of the list you should do:

b = a[:]

Names can also point to functions, class instances, class methods or any other 
object. Example:

def foo(arg):
    print arg


bar = foo

Now you can call bar(arg) or foo(arg) and it will do exactly the same thing.

If you ever wrote assembler than you will begin to understand.  varibles are 
pointers to objects, not buckets were stuff is stored.

Hope this helps some.

-Larry



More information about the Python-list mailing list