enhancing decorator signatures

Discussion in 'Python' started by Diez B. Roggisch, Aug 6, 2008.

  1. Hi,

    I'm using Michele S's decorator-module to create decorators with matching
    signatures, for better error-catching.

    However, I now want to enrich the signature of a generic wrapper so that the
    new function will accept more parameters (keyword only). These additional
    parameters are consumed by the wrapper and not passed to the decorated
    function.

    So something like this would be cool:

    @enriched_decorator("bar")
    def some_decorator(f, **args, **kwargs):
    bar = kwargs.pop("bar")
    return f(**args, **kwargs)

    Anybody has done something like this?

    Diez
     
    Diez B. Roggisch, Aug 6, 2008
    #1
    1. Advertisements

  2. Diez B. Roggisch

    castironpi Guest

    Diez,

    notfound= object()
    @enriched_decorator("bar")
    def some_decorator(f, *args, **kwargs):
    bar = kwargs.pop("bar",notfound)
    return f(*args, **kwargs)

    Then you would declare your function:

    @some_decorator
    def funA( x, y, z ):
    do( x, y )
    make( z )

    And be able to call it:

    funA( 0, 1, 2, bar= 'what' )

    so some_decorator consumes 'bar' and preserves x, y, and z. It's
    working fine. What did you want to ask about?

    --Roggisch has plonked me in the past. Can someone reply to this
    message to the group so they'll see it?--
     
    castironpi, Aug 6, 2008
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.