Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'argument': 0.05; 'modify': 0.07; '__init__': 0.09; 'arguments': 0.09; 'decorator': 0.09; 'default.': 0.09; 'deprecated': 0.09; 'method,': 0.09; 'used.': 0.09; 'python': 0.11; 'jan': 0.12; '"hello': 0.16; '*args': 0.16; 'argument.': 0.16; 'non-default': 0.16; 'subject:based': 0.16; 'subject:class': 0.16; 'unbound': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'appears': 0.22; 'module,': 0.24; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'inspect': 0.31; 'prints': 0.31; 'values.': 0.31; 'class': 0.32; 'fri,': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'hi,': 0.36; 'should': 0.36; 'checks': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'here:': 0.62; "you'll": 0.62; 'complete': 0.62; 'such': 0.63; 'map': 0.64; 'more': 0.64; 'default': 0.69; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=CY9Pi9FKzG+Qvm+xKcwfYiFCVzD78FGzycideSn88PU=; b=osHtngnSFaMcvmhpXOMaopaRemzM151H++yUfsbI8Wt0axX7UgQewXzfKEIjtAnu6k LvmfejdU87XI1+naeM2EwxtSFkLDCYHIDWanZwuZu+5xWqbCTy8qwF9mZB4g3mQSnDVG TPWUYgz/uCIg7y9sXweCTvwa/2LEcjec3LiifCq4f26lbM5KiW6FRYjifRVW/mQyZVOb CEe605Nqjlc5kNJgNMhrzVdwMnBe7fw4DBXp5UxFkbXf2R2+KGQ2NAFulPIYiMgrP17q dzU49oapb0g0ZxpKmIYHTgevTr2UIkLgaRvQDRmmMOed/aGgtoQTsXfcdMQXIs87toPb dNPg== X-Received: by 10.66.231.141 with SMTP id tg13mr29693524pac.122.1420871443595; Fri, 09 Jan 2015 22:30:43 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <879539503.1660445.1420838812525.JavaMail.yahoo@jws10784.mail.gq1.yahoo.com> References: <879539503.1660445.1420838812525.JavaMail.yahoo@jws10784.mail.gq1.yahoo.com> From: Ian Kelly Date: Fri, 9 Jan 2015 23:30:02 -0700 Subject: Re: class-based class decorator To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420871452 news.xs4all.nl 2914 [2001:888:2000:d::a6]:39112 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83491 On Fri, Jan 9, 2015 at 2:26 PM, Albert-Jan Roskam 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.