More about variables

Tom Culliton culliton at clark.net
Thu Apr 6 18:52:57 EDT 2000


In article <meh9-158D1A.14240006042000 at news.cit.cornell.edu>,
Matthew Hirsch  <meh9 at cornell.edu> wrote:
>Why create named variables?
>
>I always thought that having 20 lists saved more space than having one 
>list of 20 lists.  Maybe I'm wrong.

You are, but it's an honest mistake since Python is different from
many common languages here.  Every Python "variable" is really a
dictionary entry, which means a string for the name, the data, and a
pair of references to them.  So a list of lists saves you the string,
one of the references, and lets you deal with the data more uniformly
making it a win all the way around.

You should probably dig through some of the online docs and tutorials
to get a better handle on how Python represents things internally and
how "variable" names and assignment really work.  Rather than names
being tightly fixed to a particular area of memory they are loosely
and indirectly associated with data objects.  An object can have many
different names during it's lifetime (sequentially or in parallel),
and a name can (sequentially) refer to many different objects.  There
are really no such thing as variables in python only objects and
their (more or less temporary) names.

For example: (the id() function essentially returns the address of the
object refered too.)

Python 1.5.2 (#1, Sep 17 1999, 20:15:36)  [GCC egcs-2.91.66 19990314/Linux (egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> x = 1
>>> y = x
>>> z = y
>>> id(x)
134882664
>>> id(y)
134882664
>>> id(z)
134882664
>>> y=2
>>> z=3
>>> id(x)
134882664
>>> id(y)
134882616
>>> id(z)
134882604
>>>




More information about the Python-list mailing list