[Tutor] Is there any logic in this?

Righard/Riku van Roy pluijzer at gmail.com
Sat Sep 1 17:02:01 CEST 2007


Thanks for your explenation, so essentialy a = b, copys the pointer of a
to b rather than the actual content. This explains why a[:] does work.

Do you have an explenation why this is not the case with integers ie.

>>> a, b = 10, a
>>> b = b + 10
>>> a, b
(10, 20)

thx


Op za, 01-09-2007 te 07:50 -0700, schreef jim stockford:
>     seems to me this is an artifact of the language.
>     reading right to left:
> "make a list that contains 10,40,30,20, then create a
> name 'a' to be used as a label to identify that list, then
> (next line) create a label 'b' to attach to whatever is
> the thing 'a' refers to, then (next line) modify the thing
> via 'b' (e.g. b.sort)."
>     the essence is to allow multiple names for things
> without clogging up memory with a lot of copies and
> to have a uniform mechanism of referencing anything
> (i.e. any "object", for everything in Python is an object,
> hence the utility of a uniform mechanism.
>     the effect is programmers have to know this is the
> case. those who have the old style "C head" using the
> model of a variable name representing an area in
> memory where assignment copies data to a new area
> in memory with the other variable name will get caught
> on this until they catch on.
> 
>     I'll be very grateful for any criticism of the above.
> 
> 
> 
> On Sep 1, 2007, at 6:29 AM, Righard/Riku van Roy wrote:
> 
> > If you copy a list into another variable, and then change the second
> > one, the first gets changed aswell, for example:
> >
> >>>> a = [10, 40, 30, 20]
> >>>> b = a
> >>>> b.sort()
> >>>> a
> > [10, 20, 30, 40]
> >>>> b
> > [10, 20, 30, 40]
> >
> > or:
> >
> >>>> a = [10, 40, 30, 20]
> >>>> b = a
> >>>> b[0] = 99
> >>>> a
> > [99, 40, 30, 20]
> >>>> b
> > [99, 40, 30, 20]
> >
> > this happens with dictionary's too, but not with intergers, it is not
> > that this is a problem because I can just use...
> >
> >>>> b = a[:]
> >
> > ...but I wonder if there is any logic behind this, I cannot find a
> > practical use for it, just problems.
> >
> > thx, Righard
> >
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 



More information about the Tutor mailing list