Behavior of += (was Re: [Python-Dev] Customization docs)
johnroth at ameritech.net
Wed Jun 5 07:41:47 EDT 2002
"Terry Reedy" <tjreedy at udel.edu> wrote in message
news:MCeL8.114006$%y.11961913 at bin4.nnrp.aus1.giganews.com...
> "Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote in message
> news:slrnafqppg.4ea.huaiyu at gauss.almadan.ibm.com...
> > Objects are referenced by their ids (or equivalent). Their memory
> > may or may not change when it is modified, but if it is the same
> > 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.
More information about the Python-list