From: Martin Drautzburg on
Just for the hell of it ...

I can easily define __plus__() with three parameters. If the last one is
optional the + operation works as expected. Is there a way to pass the
third argument to "+"
From: Robert Kern on
On 2010-02-08 14:59 PM, Martin Drautzburg wrote:
> Just for the hell of it ...
>
> I can easily define __plus__() with three parameters. If the last one is
> optional the + operation works as expected. Is there a way to pass the
> third argument to "+"

No.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

From: Steven D'Aprano on
On Mon, 08 Feb 2010 21:59:18 +0100, Martin Drautzburg wrote:

> Just for the hell of it ...
>
> I can easily define __plus__() with three parameters. If the last one is
> optional the + operation works as expected. Is there a way to pass the
> third argument to "+"

How do you give three operands to a binary operator? Binary operators
only have two sides, a left and a right, so you can only fit two operands
around them.

Mathematicians solve this problem by using functions:

add(a, b, c, d)

In Python, you can do this:

>>> class F:
.... def __add__(self, other, foo=None):
.... print self, other, foo
.... return 1
....
>>>
>>> F() + 3
<__main__.F instance at 0xb7f06f4c> 3 None
1
>>> F().__add__(3, 4)
<__main__.F instance at 0xb7f06d8c> 3 4
1



but if you do, people will laugh and point at you in the street.

*wink*


--
Steven
From: Carl Banks on
On Feb 8, 12:59 pm, Martin Drautzburg <Martin.Drautzb...(a)web.de>
wrote:
> Just for the hell of it ...
>
> I can easily define __plus__() with three parameters. If the last one is
> optional the + operation works as expected. Is there a way to pass the
> third argument to "+"

If, for some reason, you wanted to define a type for which it makes
sense to "add" three objects, but not two, you can get the effect you
want, kind of.

(a + b + c) is useful
(a + b) is meaningless

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.


Carl Banks
From: Martin Drautzburg on
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
insight>

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.