Ternary plus

Martin Drautzburg Martin.Drautzburg at web.de
Tue Feb 9 19:47:43 CET 2010

Carl Banks wrote:

> You can have __add__ return a closure for the first addition, then
> perform the operation on the second one.  Example (untested):
> class Closure(object):
>     def __init__(self,t1,t2):
>         self.t1 = t1
>         self.t2 = t2
>     def __add__(self,t3):
>         # whole operation peformed here
>         return self.t1 + self.t2 + t3
> class MySpecialInt(int):
>     def __add__(self,other):
>         return Closure(self,other)
> I wouldn't recommend it.  Just use a function call with three
> arguments.

That's way cool. 

<Flash of insight> Of course! - CURRYING!! If you can return closures
you can do everything with just single-parameter functions.</Flash of

BTW I am not really trying to add three objects, I wanted a third object
which controls the way the addition is done. Sort of like "/" and "//"
which are two different ways of doing division.

Anyways: thanks a lot.

More information about the Python-list mailing list