inconsistency with += between different types ?

Jonathan Hogg jonathan at onegoodidea.com
Tue Aug 6 04:16:27 EDT 2002


On 5/8/2002 23:54, in article slrnaku0ge.116.huaiyu at gauss.almadan.ibm.com,
"Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote:

> That's what it should be.  Yet it is possible for whoever writes __iadd__ to
> define it the other way.  I think this is exactly Donn's point: that += was
> designed in such a way that both semantics are allowed under the same
> notation.

You're always at the mercy of a module author to define sensible semantics
for operators. Unfortunately, that's life. __iadd__ has to be able to
support both models to support mutable and non-mutable objects. Which
semantics you choose is a judgement call on what the users of the type would
expect.

But you've missed the most obvious concern: if you don't define __iadd__ but
you do define __add__ then you automatically get a version of __iadd__ that
uses re-assignment, even if you're a mutable object ;-)

Jonathan




More information about the Python-list mailing list