[Tutor] Is there any logic in this?

jim stockford jim at well.com
Sat Sep 1 16:50:53 CEST 2007


    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