Behavior of += (was Re: [Python-Dev] Customization docs)

John Roth johnroth at
Wed Jun 5 07:41:47 EDT 2002

"Terry Reedy" <tjreedy at> wrote in message
news:MCeL8.114006$%y.11961913 at
> "Huaiyu Zhu" <huaiyu at> wrote in message
> news:slrnafqppg.4ea.huaiyu at
> > Objects are referenced by their ids (or equivalent).  Their memory
> location
> > may or may not change when it is modified, but if it is the same
> object,
> > every name that was refering to it will remain referencing to it
> after the
> > modification, regardless of its memory location.
> Conceptually, PyObjects do not even *have* a memory location.  In the
> current CPython implementation for computers, they have a *fixed*
> location, which happens to be used for the unique integer id.  A
> (fixed-location) list object contains a pointer to an auxiliary array
> which may be moved when resized.  Same for dicts.
> Terry J. Reedy

I see your point, which explains why the list in the first
example always updates. The issue then seems to be that
in cases where the '+=' operation returns the same object
it started with, the generated code still tries to update
the container object, causing an exception if the
container object happens to be immutable.

John Roth


More information about the Python-list mailing list