what is the keyword "is" for?
"Martin v. Löwis"
martin at v.loewis.de
Tue Aug 15 04:16:26 EDT 2006
daniel wrote:
> when I tried to check the stuff out, found sth interesting that if you
> define variables in a style like this:
> a = b = ['a', 'b']
> changing one list affects the other, and they still refer to same
> object. in fact, seems all compound types (dictionary for instance)
> behave in this way.
>
> however, when list is replaced with other built-in types like integers
> :
> a = b = 3
> changing one of them cause the two objects differ...
Ah, but make a difference between "change a variable", and "change an
object".
py> a = b = [1,2,3]
py> a[0] = 6 # don't change the variable a, just change the object
py> a
[6, 2, 3]
py> b
[6, 2, 3]
py> a=[7,8,9] # change the variable a;
# it's now a different object than b
py> a
[7, 8, 9]
py> b
[6, 2, 3]
For some objects, "change the object" is impossible. If you have
a = b = 3
then there is no way to change the object 3 to become 4 (say);
integer objects are "immutable". So for these, to make a change,
you really have to change the variable, not the value.
Regards,
Martin
More information about the Python-list
mailing list