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

Ned Batchelder ned at nedbatchelder.com
Sun Sep 29 21:35:27 CEST 2013


On 9/29/13 11:06 AM, Xavier Morel wrote:
> 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.

It isn't just a matter of a more complex parser: where would the parser 
get the information about these new operators?  The obvious first answer 
is that they would be defined as part of classes, but that means the 
operator definition is in an imported module some place.  The importing 
module couldn't even be parsed until the class was imported.  This is a 
completely different compilation and execution model than Python has now.

Now you can compile a Python module without any access to the modules it 
will eventually import.  Importing is a run-time operation.  If you want 
user-defined classes to be able to define new syntax, then importing has 
to happen at compile time.  That's a completely different language.

--Ned.


More information about the Python-Dev mailing list