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

John Roth johnroth at ameritech.net
Wed Jun 5 07:51:49 EDT 2002


"Delaney, Timothy" <tdelaney at avaya.com> wrote in message
news:mailman.1023234674.21472.python-list at python.org...
> > From: Jeff Epler [mailto:jepler at unpythonic.net]
> >
> > On Tue, Jun 04, 2002 at 12:01:56PM -0400, Steve Holden wrote:
> > > >>> a = b = [1,2,3]
> > > >>> a += [4]
> > > >>> a, id(a), b, id(b)
> > > ([1, 2, 3, 4], 269472088, [1, 2, 3, 4], 269472088)
> > > >>>
> > >
> > > Where did the rebinding take place? ISTM that "a" is still
> > bound to the same
> > > list, which has been modified in place.
> >
> > Sure it rebinds a (just to the same object formerly bound to it) ..
> > >>> class X:
> > ...     def __setattr__(self, a, v):
> > ...         print "rebinding %s, sucker" % a
> > ...         self.__dict__[a] = v
> > ...
> > >>> x = X()
> > >>> x.a = b = [1,2,3]
> > rebinding a, sucker
> > >>> x.a += [4]
> > rebinding a, sucker
> > >>> x.a is b
> > True
>
> To put it another way ... (for __iadd__, assume every inplace
assignment
> method).
>
> __iadd__ must return an object (or None). The result of __iadd__ is
rebound
> to the original name.
>
> For an object that modifies in place (such as a list), it returns self
from
> __iadd__.
>
> So there definitely is rebinding going on with every augmented
assignment.
>
> The problem is that the rebinding occurs *after* __iadd__ is called,
and I
> can't see any way to avoid that. Yes, you can refuse to rebind, but
any side
> effects have already occurred.
>
> Tim Delaney

It mostly depends on what we want to have happen in
this case. I see three distinct possibilities:

1. Forbid the operation when the container is immutable,
and the embedded object does not update in place.
2. Don't attempt to update an immutable container.
3. Don't attempt to update when the embedded operation
returns itself.

Each of these require additional overhead.

1 requires two checks, on the container and on
the contained object.

2 requires only one check, on the container object

3 requires a check on the object returned from the
embedded operation.

I don't see a compatability issue here, because
the current behavior raises an exception.

John Roth
>

>





More information about the Python-list mailing list