[Python-Dev] Why not support user defined operator overloading ?

Xavier Morel python-dev at masklinn.net
Sun Sep 29 17:06:32 CEST 2013


On 2013-09-29, at 14:51 , 张佩佩 wrote:

> Hello:
> 	As far as I know, there is not a language support user defined operator overloading.
> Python3 can overloading belowed operators.
> -	negated
> +	unchanged
> 
> -	minus
> +	add
> *	multiplication
> /	division
> //	true division
> %	remainder
> **	power
> (Do I miss something ?)

~         invert (unary)
()        call
.         get attribute
[]        get item
<<        left shift
>>        right shift
&         binary and
^         xor
|         binary or

And the inplace versions of most of these can be implemented separately,
which can probably be counted as supplementary operators.

> 
> If we can overloading these operators, why we can't overloading other operators?
> (like .* often used in matrix, U in set operation)

This is more of a python-ideas subject.

And one of the reasons likely is that it would require significantly
reworking the grammar to handle a kind of user-defined opname (similar
to name, but for operator tokens), with user-defined priority and
associativity, and the ability to import operators (or define how and
when operators become available compared to their definition)

That's a huge amount of complexity with little to gain.


More information about the Python-Dev mailing list