[Python-ideas] returning anonymous functions
Mathias Panzenböck
grosser.meister.morti at gmx.net
Tue Dec 16 09:40:17 CET 2008
Mathias Panzenböck schrieb:
> Scott Dial schrieb:
>> Is is really that common? In this case, you are misrepresenting the
>> pattern. The appropriate version of this would require references to _f
>> to make it's signature match that of f's, and therefore this entire
>> argument is specious. In reality, the number of times you can get away
>> with returning a truly anonymous function (that isn't a glorified
>> lambda) is rare, I think.
>>
>> def deco(f):
>> def _f(*args,**kawrgs):
>> ...
>> functools.update_wrapper(_f, f)
>> return _f
>>
>> Or:
>>
>> def deco(f):
>> @functools.wraps(f)
>> def _f(*args,**kawrgs):
>> ...
>> return _f
>>
>> Even in the second case, it would be awkward to inline with the return
>> statement because of the need to invoke a decorator.
>>
>
>
> ic.
>
> Ok, maybe something even more different: curried functions.
>
> def plus2(f)(x):
> return f(x+2)
>
> @plus2
> def foo(x):
> ...
>
> Or I don't know. Just a thought. And yes, you cannot user functools.wraps on
> that either. And this is just plain ugly/utter crap:
>
> def plus2(f)
> @functools.wraps(f)
> def (x):
> return f(x+2)
>
>
> -panzi
Thinking of it, we do not need any new syntax:
def curry(f):
def _f(x).
def _f2(*args,**kwargs):
return f(x,*args,**kwargs)
return _f2
return _f
@curry
def foo(a,b,c):
return a+b+c
Maybe we can somehow also use functools.update_wrapper here.
-panzi
More information about the Python-ideas
mailing list