Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83491
| References | <879539503.1660445.1420838812525.JavaMail.yahoo@jws10784.mail.gq1.yahoo.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2015-01-09 23:30 -0700 |
| Subject | Re: class-based class decorator |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17553.1420871452.18130.python-list@python.org> (permalink) |
On Fri, Jan 9, 2015 at 2:26 PM, Albert-Jan Roskam <fomcl@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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: class-based class decorator Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-09 23:30 -0700
csiph-web