[Python-ideas] Reverse assignment operators (=+, =-, =*, =/, =//, =**, =%)

Matthias welp boekewurm at gmail.com
Mon Nov 14 21:59:28 EST 2016


>> For a mutable class, A = A + something is fundamentally different from
>> A += something.
>>
>> Paul

> Ok I am calmed down already.
> But how do you jump to lists already? I started an example with integers.
> I just want to increment an integer and I don't want to see any += in my code,
> it strains me. And that is exact reason I hate C syntax.
> Then Todd started about Numpy arrays. Ok, I just commented what I find for
> clearer syntax with array increment.
> And now lists, mutable classes... I don't use classes in my programs.
> I could propose something but how, if we mix everything in one big pile?
> Then my proposal is to make typed variables first, so I could
> at least consider use cases.

> Mikhail

Mikhail, what Paul probably means here is that python 'operators' are actually
'syntactic sugar' for functions (it is not recommended to call
these functions directly, but it is possible):

e.g. if you use 'a = a + 1', python under the hood interprets it as
'a = a.__add__(1)', but if you use 'a += 1' it uses 'a.__iadd__(1)'.
All python operators are implementable under functions, which is
part of the spec. For integers or strings, that may not be as visible to the
end user, but if you want to change the behaviour of operators,
please first look at the docs [1] and try to understand them, and
understand why they exists (inconclusive examples: [2).

I hope that this clears up the misconceptions you may have about how python
operators work.

-Matthias

[1]: https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
[2]: e.g. to make your library more efficient (like the operators in
numpy for more efficient array operations), or adding operator
functionality for numerical classes (like the decimal.Decimal class).


More information about the Python-ideas mailing list