class-based class decorator

Albert-Jan Roskam fomcl at yahoo.com
Thu Jan 15 22:07:09 CET 2015



----- Original Message -----

> From: Jean-Michel Pichavant <jeanmichel at sequans.com>
> To: Albert-Jan Roskam <fomcl at yahoo.com>
> Cc: Python <python-list at python.org>
> Sent: Tuesday, January 13, 2015 5:37 PM
> Subject: Re: class-based class decorator
> 
> ----- Original Message -----
>>  From: "Albert-Jan Roskam" <fomcl at yahoo.com>
>>  > From: Jean-Michel Pichavant <jeanmichel at sequans.com>
>>  > I don't really understand how you successfuly manage positional
>>  > parameters,
>>  > since the caller may not name them.
>>  > I'm asking because if your intend to check only the keyword
>>  > parameters,
>>  > there's a much simplier solution.
>>  > 
>>  > JM
>> 
>> 
>>  Hi,
>> 
>>  Can you give an example of where/how my code would fail? I do not
>>  intend to use *args and **kwargs, if that is what you mean. I am
>>  interested in hearing a simpler approach, especially if it would
>>  also solve the messed-up-signature problem that I mentioned.
>> 
>>  Thank you!
>> 
>>  Albert-Jan
>> 
> 
> In the example you've given you've deprecated only keyword arguments. So 
> I was wondering...
> 
> would the following *untested* code work ? :
> 
> def check_deprecated_args(deprecated):
>   def decorator(func):
>     def inner(*args, **kwargs):
>       for p in deprecated:
>         if p in kwargs:
>           print '%s is deprecated' % p
>       return func(*args, **kwargs)
>     return inner
>   return decorator
> 


This is indeed a lot simpler than a class-based version, merci bien! And now I can also make it work with the signature-preserving decorator package. Meanwhile I also found a Python 2 backport for inspect.signature, so positional args can also be used. 


Thanks again!

Albert-Jan



More information about the Python-list mailing list