can I overload operators like "=>", "->" or something like that?

Chris Angelico rosuav at gmail.com
Fri Apr 20 00:43:09 EDT 2012


On Fri, Apr 20, 2012 at 2:38 PM, alex23 <wuwei23 at gmail.com> wrote:
> On Apr 20, 5:54 am, Jacob MacDonald <jaccar... at gmail.com> wrote:
>
>> On Thursday, April 19, 2012 12:28:50 PM UTC-7, dmitrey wrote:
>> > can I somehow overload operators like "=>", "->" or something like
>> > that?
>
>> I don't believe that you could overload those particular operators,
>> since to my knowledge they do not exist in Python to begin with.
>
> It all depends on if the operators use special methods on objects:
> http://docs.python.org/reference/datamodel.html#special-method-names
>
> You can overload => via object.__le__, for example.

Yes, but it will be a comparison operator, and to an extent, will be
assumed to function as one. I don't recommend abusing comparisons for
other operations - you'll confuse people no end.

ChrisA



More information about the Python-list mailing list