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

MRAB python at mrabarnett.plus.com
Sun May 26 21:00:11 EDT 2019


On 2019-05-26 21:34, Yanghao Hua wrote:
> On Sun, May 26, 2019 at 10:25 PM Chris Angelico <rosuav at gmail.com> wrote:
>>
>> 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.
> 
> Absolutely right and good. But if you just had := or <== mean
> "assignment behavior is handover to the object if __assign__ is
> defined", then you can remove the whole descriptors thing (or rather,
> it can do all a descriptor can and better, and actually := or <== is
> saying, hey look, I am a descriptor ... ) . ;-)
> 
> I am not saying "=" should be overloaded, I am saying there should be
> an assignment that can be overloaded, which completes the picture in
> python.
> 
It seems to me that this is a little like "variables" in tkinter 
(IntVar, StringVar, etc.). You create and then set and get the 
contents/value, but in tkinter there's always a clear distinction 
between the variable itself and its value. For example, you don't say 
"var + 1", but "var.get() + 1".

However, in the examples that you've given, the variables on the RHS are 
being used in calculations in the same way that the value would be used, 
e.g. "var + 1" (basically "var.get() + 1"), which means that those 
operations need to be defined in the classes, but one operation that 
cannot be defined as an operator is setting the value. It's not possible 
to write "var.set(var.get() + 1)" as, say, "var <== var + 1".

I'm not sure what I think about that, but given that you're willing to 
define a whole load of other operators...  Hmm...  Not sure.


More information about the Python-ideas mailing list