Decorators, Identity functions and execution...
Fredrik Lundh
fredrik at pythonware.com
Sun Apr 9 03:51:18 EDT 2006
Chance Ginger" wrote:
> If I define a decorator like:
>
> def t(x) :
> def I(x) : return x
> return I
... you get a syntax error.
> and use it like:
>
> @t(X)
> def foo(a) :
> # definition of foo...
> pass
that's also a syntax error.
> or maybe this:
>
> @t(X)
> @(Y)
> def bar(a) :
> # The definition of bar...
and that's not even fixable.
> Will in encounter much of a penalty in executing
> 'foo' or 'bar'?
since foo is wrapped, calling foo will call your I function, which in
turn calls the original foo.
> If so, is there a way to define t such that Python knows it is
> the identity function and short-circuit evaluation?
if you don't want to wrap something, don't wrap it:
def t(x) :
def I(x) :
return x
if date == friday:
return x # don't wrap it
return I # wrap it
</F>
More information about the Python-list
mailing list