[Tutor] assignment statements in python
Python
python at venix.com
Mon Jun 12 04:59:04 CEST 2006
On Sun, 2006-06-11 at 22:14 -0400, Kermit Rose wrote:
> Message: 1
> Date: Sun, 11 Jun 2006 06:58:39 -0400
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] buggy bug in my program
> Cc: tutor at python.org
>
> Assignment in Python is not a copy, it is a name binding. Assignment
> creates a name for an object. If you assign the same object to two
> names, they both are bound to the same thing. If the object is mutable,
> like a list, changes to the object will be seen regardless of which name
> you use to refer to it.
>
> ******
>
> I feel a little bit better now that I know that there is a reason for what
> my
> program did.
>
> However, I still don't have any idea how to copy values from one cell in
> an array to the adjacent cell in the same array.
>
> I looked at the reference ,
>
> http://www.effbot.org/zone/python-objects.htm
>
> that you gave,
>
> but did not gleam any hint from it how to copy values from one place in an
> array to another place within the same array.
>
> It must be possible, for otherwise, you could not sort an array.
>
>
> It is quite remarkable that my not knowing that
>
> assignment is not a copy
>
> gave me no difficulties before now.
The basic python objects: numbers, strings, and tuples are immutable and
can not be changed (mutated).
a = b = 3
b = 4 # binds b to a different object with value 4
# the object with value 3 is unchanged
print a
3
Container objects such as lists and dictionaries can be changed in
place.
>>> a = b = [1,2,3]
>>> b.append(4) # changes (mutates) b
>>> print a
[1, 2, 3, 4]
>>> b[2] = b[3] # positions 2 and 3 reference the same object
>>> print b
[1, 2, 4, 4]
>>> print a
[1, 2, 4, 4]
>
>
>
> Kermit < kermit at polaris.net >
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list