Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Signature-preserving decorators

Reply
Thread Tools

Signature-preserving decorators

 
 
Henrik Faber
Guest
Posts: n/a
 
      12-13-2011
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

 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      12-13-2011
On Tue, Dec 13, 2011 at 6:36 AM, Henrik Faber <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decorators v Inheritance: Forcing subclass to call super() ? Michael Strorm Java 12 04-20-2005 08:05 PM
PEP 318 decorators are not Decorators Arien Malec Python 11 08-16-2004 06:38 PM
RE: Using metaclasses to play with decorators. Robert Brewer Python 9 06-26-2004 12:58 AM
Using metaclasses to play with decorators. Michael Sparks Python 4 06-18-2004 06:25 AM
Re: Using metaclasses to play with decorators. Michael Sparks Python 6 06-18-2004 05:04 AM



Advertisments