Is behavior of += intentional for int?

Piet van Oostrum piet at cs.uu.nl
Tue Sep 1 13:40:38 EDT 2009


>>>>> zaur <szport at gmail.com> (z) wrote:

>z> On 29 авг, 16:45, zaur <szp... at gmail.com> wrote:
>>> Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
>>> [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
>>> Type "copyright", "credits" or "license()" for more information.>>> a=1
>>> >>> x=[a]
>>> >>> id(a)==id(x[0])
>>> True
>>> >>> a+=1
>>> >>> a
>>> 2
>>> >>> x[0]
>>> 
>>> 1
>>> 
>>> I thought that += should only change the value of the int object. But
>>> += create new.
>>> Is this intentional?

>z> As a result of this debate is not whether we should conclude that
>z> there should be two types of integers in python: 1) immutable numbers,
>z> which behave as constant value; 2) mutable numbers, which behave as
>z> variable value?

Numbers are immutable by nature (math). The number 3.14 remains 3.14
whatever you try to do with it. What you call an immutable number is in
fact a container that contains a number. You can change the contents of
the container, not by modifying the number in it but by replacing it
with a different number. Python has sufficient mechanisms for creating
these containers: lists, dictionaries, objects. If you think they are
not good enough then write a new one in C.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list