Ternary plus
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Mon Feb 8 20:49:39 EST 2010
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
More information about the Python-list
mailing list