Decorator
Martin Blume
mblume at socha.net
Fri May 12 10:05:52 EDT 2006
"bruno at modulix" schrieb
>
> What Python 2.4 adds is only syntactic sugar for decorators.
> You can do the same - somewhat more explicitely - in 2.3.
>
> > What is the decorator useful for?
>
>
> The whole things looks like this:
>
> def deco(func):
> print "decorating %s" % func.__name__
> def _wrapper(*args, **kw):
> print "%s called " % func.__name__
> res = func(*args, **kw)
> print "%s returned %s" % (func.__name__, str(res))
return res
^^^^^^^^^^
Shouldn't here be a return res, so that wrapper
behaves like the original function?
> return _wrapper
>
> # python < 2.4
> def somefunc():
> print "in somefunc"
> return 42
>
> somefunc = deco(somefunc)
>
Thanks for the explanation.
Another question: Isn't decorating / wrapping usually
done at runtime, so that the @deco notation is pretty
useless (because you'd have to change the original
code)?
What do I miss here?
Martin
More information about the Python-list
mailing list