[Python-ideas] returning anonymous functions

Mathias Panzenböck grosser.meister.morti at gmx.net
Mon Dec 15 20:12:49 CET 2008


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



More information about the Python-ideas mailing list