class-based class decorator

Ian Kelly ian.g.kelly at gmail.com
Sat Jan 10 07:30:02 CET 2015


On Fri, Jan 9, 2015 at 2:26 PM, Albert-Jan Roskam
<fomcl at yahoo.com.dmarc.invalid> wrote:
>
>
> Hi,
>
> I am trying to write a class decorator that checks whether deprecated parameters with non-default
>
> arguments are used. More complete code is here: http://pastebin.com/ZqnMis6M. In the code below,
>
> how should I modify __call__ such that f.bar(old="oh no") prints "hello world"?
> I thought it would be a fun way to play with the inspect module, but my head is numb now and I am thirsty for a beer!

You can use inspect.getargspec to look up the default argument values
for the target function. You can use inspect.getcallargs to map your
*args and **kwargs to a dictionary of argument names and values. Since
the target function will be the unbound __init__ method, you'll also
want to pass in a dummy value like None for the self argument. Then
you just look up each of the deprecated names in the result and flag
any where the value doesn't match the default.

If you were using Python 3.3+, then I would recommend using
inspect.signature instead, which is a lot more powerful. But your code
appears to be Python 2, so we work with what we've got.



More information about the Python-list mailing list