Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17135 > unrolled thread
| Started by | Henrik Faber <hfaber@invalid.net> |
|---|---|
| First post | 2011-12-13 14:36 +0100 |
| Last post | 2011-12-13 09:26 -0700 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Signature-preserving decorators Henrik Faber <hfaber@invalid.net> - 2011-12-13 14:36 +0100
Re: Signature-preserving decorators Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-13 09:26 -0700
| From | Henrik Faber <hfaber@invalid.net> |
|---|---|
| Date | 2011-12-13 14:36 +0100 |
| Subject | Signature-preserving decorators |
| Message-ID | <jc7kd4$sak$1@speranza.aioe.org> |
Hi group, when decorating a method in Python3, by use of the functools.update_wrapper function, it can be achieved that the docstring and name of the original function is preseverved. However, the prototype is lost: When looking into the Python help, I have lots of entries that look like: getfoo(*args, **kwargs) -> int setbar(*args, **kwargs) As you can imagine, this is really not very self-explanatory. I've seen a solution which constructs a wrapper's wrapper function using inspection and eval -- this looks really dirty to me, however. Then there's the "decorator" external module -- but I'd like to do it with on-board tools. Is this possible in Python3 with too much of a hassle? Best regards, Joe
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-12-13 09:26 -0700 |
| Message-ID | <mailman.3605.1323793653.27778.python-list@python.org> |
| In reply to | #17135 |
On Tue, Dec 13, 2011 at 6:36 AM, Henrik Faber <hfaber@invalid.net> wrote: > Hi group, > > when decorating a method in Python3, by use of the > functools.update_wrapper function, it can be achieved that the docstring > and name of the original function is preseverved. > > However, the prototype is lost: When looking into the Python help, I > have lots of entries that look like: > > getfoo(*args, **kwargs) -> int > > setbar(*args, **kwargs) > > As you can imagine, this is really not very self-explanatory. I've seen > a solution which constructs a wrapper's wrapper function using > inspection and eval -- this looks really dirty to me, however. Then > there's the "decorator" external module -- but I'd like to do it with > on-board tools. > > Is this possible in Python3 with too much of a hassle? The decorator module also uses inspection and eval to do it, by the way. Currently there is no pretty way to do it that I know of, but see PEP 362: http://www.python.org/dev/peps/pep-0362/ That PEP has unfortunately been in the "Open" state for quite a long time now, but it seems to me that a lot of people are starting to get interested in this issue, so maybe it will start to pick up some steam before too long.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web