Static variable vs Class variable

Hrvoje Niksic hniksic at
Wed Oct 17 13:41:06 CEST 2007

Duncan Booth <duncan.booth at invalid.invalid> writes:

> Hrvoje Niksic <hniksic at> 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)
>>> 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.

More information about the Python-list mailing list