enhancing decorator signatures

castironpi castironpi at gmail.com
Wed Aug 6 12:31:51 EDT 2008


On Aug 6, 7:16 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> 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,

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?--



More information about the Python-list mailing list