On Wed, 17 Oct 2007 13:41:06 +0200, Hrvoje Niksic wrote: > Duncan Booth <[EMAIL PROTECTED]> writes: > >> Hrvoje Niksic <[EMAIL PROTECTED]> wrote: >> >>> I've recently been bitten by [rebinding the var to what __iadd__ >>> returns], and I don't understand the reasoning behind __iadd__'s >>> design. I mean, what is the point of an *in-place* add operation >>> (and others) if it doesn't always work in-place? >>> >> A very common use case is using it to increment a number: > > I'm aware of that; but remember that there's still __add__. It would > be sufficient for numbers not to implement __iadd__. And, in fact, > they already don't: > >>>> 1 .__add__(1) > 2 >>>> 1 .__iadd__(1) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > AttributeError: 'int' object has no attribute '__iadd__' > > The current implementation of += uses __add__ for addition and > __iadd__ for addition that may or may not be in-place. I'd like to > know the rationale for that design.
Simply not to introduce special cases I guess. If you write ``x.a += b`` then `x.a` will be rebound whether an `a.__iadd__()` exists or not. Otherwise one would get interesting subtle differences with properties for example. If `x.a` is a property that checks if the value satisfies some constraints ``x.a += b`` would trigger the set method only if there is no `__iadd__()` involved if there's no rebinding. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list