Parameterized Functions without Classes
Peter Otten
__peter__ at web.de
Fri Jun 25 17:32:04 EDT 2004
Christopher T King wrote:
> On Fri, 25 Jun 2004, Christian Tismer wrote:
>
>> Lambdas were not the topic, since they are a deprecated
>> feature, but sure it works.
>
> Since when are lambdas deprecated? And if so, why?
>
> IMHO, the best way to parameterize functions is with 2.4's proposed
> partial() function:
>
> def converter(factor,val):
> return val*factor
>
> inchtocm=partial(converter,2.54)
> cmtoinch=partial(converter,1/2.54)
>
> where a handy pre-2.4 definition of partial is:
>
> def partial(func,*args,**kw):
> return lambda *a,**k: func(*(args+a),**dict(kw.items()+k.items()))
In what way is that different from using an ordinary function?
def partial(func, *args, **kw):
def call(*a, **k):
return func(*(args+a), **dict(kw.items()+k.items()))
return call
Of course, the use of functions is not limited like lambdas
because (fortunately) they can contain more than one single
expression. But other than that, it's pretty much the
same concept, isn't it?
:-)
My-newsreader-doesn't-support-circular-threads-yetly yours,
Peter
More information about the Python-list
mailing list