parse-time optimizations

jcm grumble at usa.net
Thu May 31 17:25:24 EDT 2001


Roeland Rengelink <r.b.rigilink at chello.nl> wrote:

> jcm> I didn't realize this was possible in Python.  Can you give an
> jcm> example of how a new, right-associative addition operator could
> jcm> be defined?

> well, more asocial than associative, but:

> class X:
>     def __add__(self, other):
>         print 'Hi', other
>         return X()

> x = X()
> y = x+1+2+3
> print y

> gives:

> Hi 1
> Hi 2
> Hi 3
> <__main__.X instance at 0x80cefa4>

> and shouldn't give

> Hi 6
> <__main__.X instance at 0x80cefa4>

Sure, this was covered already--plus is left-associative.  Another
poster implied that in Python you can redefine plus so that it is
right-associative for a custom type.  Or maybe I misinterpreted him.



More information about the Python-list mailing list