[Python-Dev] Decorators and docstrings (original) (raw)
Roman Suzi rnd at onego.ru
Sun Aug 15 10:50:53 CEST 2004
- Previous message: [Python-Dev] Decorators and docstrings
- Next message: [Python-Dev] Decorators are function/method subclassing technique?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sat, 14 Aug 2004, Bob Ippolito wrote:
On Aug 14, 2004, at 10:26 PM, Hans Nowak wrote:
When playing with the decorators in 2.4a2, I saw the following behavior:
# wraps an object with the function, and prints this object # when the function is called. >>> def wrapwith(obj): ... def decorator(f): ... def wrapper(*args, **kwargs): ... print "##", obj ... return f(*args, **kwargs) ... return wrapper ... return decorator ... >>> @wrapwith("hello") ... def foo(x, y): ... """ foo! """ ... return 2*x + y ... >>> foo(4, 5) ## hello 13 >>> foo.doc >>> I.e. if you wrap the function foo with the decorator, it loses the docstring. foo.doc is empty. Is this behavior intended? Should the decorator in question (in this case, 'wrapwith') take care of copying of the docstring itself? Or should the decorator mechanism do this automatically (if possible)? Decorators aren't that special. If you want to return a function object that has the same doc string as the original then you're going to have to return the original object or explicitly make sure your new function object has the same doc string. wrapper.doc = decorator.doc before the return should probably do it. I don't think that automatically setting a doc string on the result object is a good idea at all because it could cause unnecessary problems for some result objects (instances of a class, for example).
Probably a decorator could be written to facilitate writing docstring- and other such info saving decorators...
def wrapwith(obj): @docsafe def decorator(f): def _wrapper(*args, **kwargs): print "##", obj return f(*args, **kwargs) return _wrapper return decorator
-bob
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/rnd%40onego.ru
Sincerely yours, Roman Suzi
rnd at onego.ru == My AI powered by GNU/Linux RedHat 7.3
- Previous message: [Python-Dev] Decorators and docstrings
- Next message: [Python-Dev] Decorators are function/method subclassing technique?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]