[Python-ideas] returning anonymous functions
Mathias Panzenböck
grosser.meister.morti at gmx.net
Mon Dec 15 19:38:21 CET 2008
It is a very common case to return a nested function, e.g. in a decorator:
def deco(f):
def _f(*args,**kwargs):
do_something()
try:
return f(*args,**kwargs)
finally:
do_something_different()
return _f
While I agree that the current way with non-anonymous functions perfectly works,
it looks ugly to a lot of people. Maybe one of these syntax variants would be an
option:
def deco(f):
return def(*args,**kwargs):
do_something()
try:
return f(*args,**kwargs)
finally:
do_something_different()
def deco(f):
return(*args,**kwargs):
do_something()
try:
return f(*args,**kwargs)
finally:
do_something_different()
def deco(f):
def return(*args,**kwargs):
do_something()
try:
return f(*args,**kwargs)
finally:
do_something_different()
Ok, the last one is not serious.
This would not be some kind of anonymous function expression but an extended
form of the return statement. Maybe you could do the same thing for yield? Well,
I guess not, because yield can have a return value and this would be awkward:
x = yield(a,b):
return a + b
And this would not be an option:
f(yield(a,b):
return a + b)
But extending return and not extending yield feels wrong.
What do you think? Does anyone have a better idea or is the current way the only
thinkable for python (which might very well be the case)?
-panzi
More information about the Python-ideas
mailing list