[Python-ideas] How we think about change [was: Reverse assignment operators...]

Chris Barker - NOAA Federal chris.barker at noaa.gov
Tue Nov 15 11:52:56 EST 2016


> Because "+" and "+=" are operators which may be defined for any
> objects.  Paul explained *why* he chose to to do that elsewhere.  My
> point is that the semantics "a += b" *is* "type(a).__iadd__(a, b)" is
> true for all objects.

Well, yes. But it is defined in particular ways in the built in types
and the stdlib. And those ways are consistent-- I.e. They do the same
thing as __add__. And we REALLY wouldn't want it any other way.

But the __i*__ operators are a bit of a wart -- they mean different
things for mutable and immutable types.

This is because they were added to satisfy two use-cases:

Syntactic sugar for common and simple operations like incrementing an integer.

Compact syntax for in-place operations.

As numbers are immutable, you can't have in-place operations. Period.
So it was handy to use the same notation for both.

And I don't think anyone wanted to add TWO new sets of operators.

This dual use does cause confusion occasionally, but not that often in practice.

-CHB


More information about the Python-ideas mailing list