Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2010, 07:59 PM
Martin Drautzburg
Guest
 
Posts: n/a
Default Ternary plus

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 "+"
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-08-2010, 08:28 PM
Robert Kern
Guest
 
Posts: n/a
Default Re: Ternary plus

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

Reply With Quote
  #3 (permalink)  
Old 02-09-2010, 12:49 AM
Steven D'Aprano
Guest
 
Posts: n/a
Default Re: Ternary plus

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
Reply With Quote
  #4 (permalink)  
Old 02-09-2010, 01:46 AM
Carl Banks
Guest
 
Posts: n/a
Default Re: Ternary plus

On Feb 8, 12:59*pm, Martin Drautzburg <Martin.Drautzb...@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
Reply With Quote
  #5 (permalink)  
Old 02-09-2010, 05:47 PM
Martin Drautzburg
Guest
 
Posts: n/a
Default Re: Ternary plus

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.



Reply With Quote
  #6 (permalink)  
Old 02-09-2010, 10:59 PM
Gabriel Genellina
Guest
 
Posts: n/a
Default Re: Ternary plus

En Tue, 09 Feb 2010 15:47:43 -0300, Martin Drautzburg
<Martin.Drautzburg@web.de> escribió:

> Carl Banks wrote:
>
>> You can have __add__ return a closure for the first addition, then
>> perform the operation on the second one. Example (untested):
>>

>
> 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.


See http://code.activestate.com/recipes/384122/ for another cool hack that
may help with that.

--
Gabriel Genellina

Reply With Quote
  #7 (permalink)  
Old 02-10-2010, 07:31 AM
Mark Dickinson
Guest
 
Posts: n/a
Default Re: Ternary plus

On Feb 9, 6:47*pm, Martin Drautzburg <Martin.Drautzb...@web.de> wrote:
> 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.


That seems like a reasonable use case for a third parameter to
__add__, though as others have pointed out the only way to pass the
third argument is to call __add__ explicitly. Here's an extract from
the decimal module:

class Decimal(object):

...

def __add__(self, other, context=None):
other = _convert_other(other)
if other is NotImplemented:
return other

if context is None:
context = getcontext()

<add 'self' and 'other' in context 'context'>

...

And here's how it's used in the decimal.Context module:

class Context(object):

...

def add(self, a, b):
"""Return the sum of the two operands.

>>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))

Decimal('19.00')
>>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))

Decimal('1.02E+4')
"""
return a.__add__(b, context=self)

--
Mark
Reply With Quote
  #8 (permalink)  
Old 02-10-2010, 07:42 AM
Mark Dickinson
Guest
 
Posts: n/a
Default Re: Ternary plus

On Feb 10, 8:31*am, Mark Dickinson <dicki...@gmail.com> wrote:
> And here's how it's used in the decimal.Context module:


Aargh! decimal.Context *class*, not module.

And it occurs to me that it would have been cleaner to have
Decimal.__add__ call Context.add rather than the other way around.
Then Decimal.__add__ could have stayed a two-argument function, as
<deity of your choice> intended. Oh well.

--
Mark
Reply With Quote
 
Reply

Popular Tags in the Forum
ternary

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ternary Operator and Arrays Felix Dominguez Newsgroup comp.lang.ruby 4 11-19-2009 07:56 PM
Ternary logic question Mark Smith Newsgroup comp.lang.php 11 10-19-2009 04:30 PM
and in ternary operator Parv G. Newsgroup comp.lang.ruby 6 06-05-2009 12:23 PM
returning value from ternary operator Sijo Kg Newsgroup comp.lang.ruby 2 04-16-2009 11:22 AM
Conditional (ternary) operators instead of IF statements stevewy@hotmail.com Newsgroup comp.lang.javascript 24 04-01-2009 08:11 AM



All times are GMT. The time now is 04:27 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.