[Python-ideas] Operator as first class citizens -- like in scala -- or yet another new operator?

Chris Angelico rosuav at gmail.com
Sun May 26 16:25:06 EDT 2019


On Mon, May 27, 2019 at 6:05 AM Yanghao Hua <yanghao.py at gmail.com> wrote:
> Doesn't matter how it ends up, I
> urge the python community do give it a second thought. (Don't you guys
> think it is odd that Python can overrides almost every operation but
> not for assignment ... is assignment really worthy being a special
> case?!)

Yes. It IS a special case, because assignment is not handled by the
object being assigned to. When you assign to an attribute or subscript
of an object, the *parent* object determines how the assignment is
done, not the one being replaced. Look at languages where the
being-replaced object gets to redefine assignment (C++ and PHP come to
mind, and there may be others). MANY MANY parts of your code become
harder to comprehend. It's not the only thing special enough to be
non-overridable. In Python, you have several fundamentals that are
absolutely guaranteed:

a = b
a is b
b if a else c
a or b # a and b

There is no way that the values of a, b, or c can change the meanings
of these expressions. (The truthiness of 'a' will define which of two
options is chosen, but you cannot redefine the operator itself.)

This is a Good Thing.

ChrisA


More information about the Python-ideas mailing list