squared functions--most Pythonic way?

Opus opus at value.net
Sun Jun 30 03:46:00 EDT 2002


Shouldn't addNumbers(5) equate to 5?  In other words, it should evaluate that 
as addNumbers(5)(0) or is that addNumbers(0)(5)?

How would you send a list of numbers (or objects that represent numbers) to 
this?

On 30 Jun 2002 at 19:24, greg wrote:

> Janto Dreijer wrote:
> > 
> > def addNumbers(k):
> >     def f(x):
> >         a = addNumbers(x + k)
> >         a.val = x+k
> >         return a
> >     return f
> > 
> > >>> addNumbers(9)(5)(2)(4)(6).val
> > 26
> > 
> > Now if only I could figure out how to use __repr__() so I don't 
> > need that ".val". It also fails when passed only one number. i.e
> > addNumbers(5). Help?
> 
> class AddNumbers:
> 
>   def __init__(self, x):
>     self.val = x
> 
>   def __call__(self, k):
>     return AddNumbers(self.val + k)
> 	
>   def __repr__(self):
>     return repr(self.val)
> 
> addNumbers = AddNumbers(0)
> 
> >>> print addNumbers(9)(5)(2)(4)(6)
> 26
> >>> print addNumbers(5)
> 0
> 
> --
> Greg
> -- 
> http://mail.python.org/mailman/listinfo/python-list


--Opus--

Dictatorship (n): a form of government under which everything 
which is not prohibited is compulsory.

--------------------------------------------------------
Get added to my Humor list:
mailto:opus at value.net?subject=ADD_HUMOR
Get added to my Neat list:
mailto:opus at value.net?subject=ADD_NEAT
Get my PGP public key:
mailto:opus at value.net?subject=PSEND&body=send%20PublicKEY.asc
Visit My Home Page:
http://value.net/~opus/








More information about the Python-list mailing list