Why does changing 1 list affect the other?

Peter Otten __peter__ at web.de
Fri Nov 7 09:02:07 CET 2003


Cy Edmunds wrote:

> Try this:
> 
>>>> x = 5
>>>> y = x
>>>> print x is y
> True
> 
> y is second reference to the immutable value 5, not a copy.

As an aside, you cannot reliably test Python's general behaviour for
immutable types with small integers (-5 <= i <= 99), as every literal in
this range will refer to the same instance:

>>> x = 5
>>> y = 5
>>> x is y
True

But:

>>> x = 100
>>> y = 100
>>> x is y
False
 
Peter




More information about the Python-list mailing list