Is behavior of += intentional for int?

Carl Banks pavlovevidence at gmail.com
Sun Aug 30 07:49:51 EDT 2009


On Aug 30, 3:34 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 30 Aug 2009 02:33:05 -0500, Derek Martin wrote:
> > On Sat, Aug 29, 2009 at 07:03:23PM +0000, Steven D'Aprano wrote:
> >> On Sat, 29 Aug 2009 11:11:43 -0700, zaur wrote:
>
> >> > I thought that int as object will stay the same object after += but
> >> > with another integer value. My intuition said me that int object
> >> > which represent integer value should behave this way.
>
> >> If it did, then you would have this behaviour:
>
> > No, you wouldn't; the behavior you described is completely different
> > from, and incompatible with, what zaur wrote.
>
> > He's saying that instead of thinking the integer value of 3 itself being
> > the object, he expected Python's object model would behave as though the
> > entity m is the object, and that object exists to contain an integer
> > value.
>
> What is "the entity m"?

I think they (Derek and zaur) expect integer objects to be mutable.

It's pretty common for people coming from "name is a location in
memory" languages to have this conception of integers as an
intermediate stage of learning Python's object system.  Even once
they've understood "everything is an object" and "names are references
to objects" they won't have learned all the nuances of the system, and
might still (not unreasonably) think integer objects could be mutable.

However, it'd be nice if all these people didn't post here whining
about how surprising and unintuitive it is and instead just said, "ah,
integers are immutable, got it", quietly to themselves.


Carl Banks



More information about the Python-list mailing list