That might be the case for more complex objects...

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Sat Apr 14 17:16:04 EDT 2007


Bart Willems wrote:

> I can try this in interactive mode:
>  >>> a = 5
>  >>> b = a
>  >>> a += 1
>  >>> print b
> 5
> 
> So, if /a/ and /b/ where pointing to the *same* "5" in memory,

They do:

>>> a = 5
>>> b = a
>>> a is b
True
>>> a += 1
>>> a is b
False

... but not after a is rebound to a new int.

> then I would expect b to be increased, just as a. But after
> increasing a, b is still 5...

int objects are immutable. Thus, when rebinding a (as happens here
in "a += 1"), a new instance of int is created and "a" points to
it.
 
> By the way, a classic language like C has features like this too;
> they're called pointers.

C's int* behaves differently.

Regards,


Björn

-- 
BOFH excuse #163:

no "any" key on keyboard




More information about the Python-list mailing list