squared functions--most Pythonic way?
Janto Dreijer
janto_d at hotmail.com
Sun Jun 30 10:30:01 EDT 2002
I think that's a typo:
>>> print AddNumbers(5)
5
How about:
reduce(lambda a,b: a(b), range(10), addNumbers)
or
reduce(apply, [[i] for i in range(10)], addNumbers)
"Opus" <opus at value.net> wrote in message news:<mailman.1025423271.21617.python-list at python.org>...
> 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
> >
More information about the Python-list
mailing list