Signature-preserving decorators

Discussion in 'Python' started by Henrik Faber, Dec 13, 2011.

  1. Henrik Faber

    Henrik Faber Guest

    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
    Henrik Faber, Dec 13, 2011
    #1
    1. Advertising

  2. Henrik Faber

    Ian Kelly Guest

    On Tue, Dec 13, 2011 at 6:36 AM, Henrik Faber <> 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.
    Ian Kelly, Dec 13, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,064
    Joerg Jooss
    Apr 24, 2004
  2. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Replies:
    11
    Views:
    562
    Arien Malec
    Aug 16, 2004
  3. TTroy
    Replies:
    16
    Views:
    780
    Peter Nilsson
    Jan 31, 2005
  4. Replies:
    4
    Views:
    507
  5. Gustavo Narea
    Replies:
    14
    Views:
    851
    Gustavo Narea
    Feb 16, 2009
Loading...

Share This Page