[Python-ideas] PEP-3150
Arnaud Delobelle
arnodel at gmail.com
Thu Apr 14 14:57:45 CEST 2011
On 14 Apr 2011, at 12:47, Carl M. Johnson wrote:
> On Wed, Apr 13, 2011 at 10:06 PM, Greg Ewing
> <greg.ewing at canterbury.ac.nz> wrote:
>
>> In this particular case (i.e. a function that does nothing
>> but define and return another function) I'd like to be able
>> to write
>>
>> def decorator(f)(*args, **kwargs):
>> do something...
>>
>> but that's a subject for another hotly-debated PEP!
>
> Interesting, but could that be extended to support making a throwaway
> key function for sorted? With given it's obviously
>
> sorted_list = sorted(original_list, key=keyfunc) given:
> def keyfunc(item):
> item = item.replace(" ", "")
> item = item.lowercase()
> ...
> return item
>
> but I can't see how that your proposal could be expanded beyond the
> one use case of decorator making, and today it's already possible to
> make a simple decorator decorator that injects f as the first arg:
>
> class MetaDec:
> def __init__(self, dec):
> self.dec = dec
>
> def __call__(self, f):
> def callme(*args, **kwargs):
> return self.dec(f, *args, **kwargs)
> return callme
You could spell that:
from functools import partial
def MetaDec(f):
return partial(partial, f)
And this is just the curry function!
--
Arnaud
More information about the Python-ideas
mailing list