references ???

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Fri Nov 2 09:00:33 EST 2001


----- Original Message ----- 
From: "Thomas Weidner" <wolf359_ at gmx.net>


> In <9rtpsc$13j2 at newton.cc.rl.ac.uk>, Richard Brodie wrote:

> > Implicitly. Consider the following:
> > 
> >>>> a = [1,2,3]
> >>>> b = a
> >>>> b[1] = 20
> >>>> print a
> > [1, 20, 3]
> 
> Interesting....
> Is this also possible for single values like ints ?

Not exactly.  Consider:

>>> a = 1       # a is reference to scalar integer 1
>>> b = a       # b is reference to same scalar integer 1
>>> b = 2       # b is rebound to scalar integer 2
>>> print a     # a is still bound where it was before
1

In effect, all Python variables are references (pointers in other
words) to objects; even the simple number 1 is stored internally
as an object.  The assignment statement, assigning to a simple
name, rebinds the name to a different object.

What you want, therefore, is the same as a reference to a reference
to a scalar object.  Python doesn't do that; generally, it's not
needed.

You will see, from time to time, mentions on this list about
thinking Pythonically.  When you master that you won't miss painful
complexities like nested references.









More information about the Python-list mailing list