Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'warnings': 0.05; 'be:': 0.09; 'module,': 0.09; 'parameter': 0.09; 'subject:create': 0.09; 'used.': 0.09; 'python': 0.11; '**kwargs)': 0.16; '**kwargs):': 0.16; 'decorator': 0.16; 'executed': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func': 0.16; 'helps!': 0.16; '\xa0def': 0.16; '\xa0print': 0.16; 'versions': 0.18; 'def': 0.20; 'received:209.85.210.174': 0.20; 'received:mail- iy0-f174.google.com': 0.20; 'wrote:': 0.21; 'function': 0.22; 'header:In-Reply-To:1': 0.22; 'message-id:@mail.gmail.com': 0.27; "i've": 0.28; 'bit': 0.28; '27,': 0.29; 'enhanced': 0.29; 'skip:@ 10': 0.29; 'though.': 0.29; 'code': 0.29; 'raise': 0.30; 'received:209.85': 0.32; 'received:209.85.210': 0.32; 'option': 0.32; 'work,': 0.32; 'received:google.com': 0.32; 'suggestion': 0.32; 'could': 0.34; 'subject:new': 0.35; 'received:209': 0.35; 'actually': 0.35; 'version': 0.36; "i'm": 0.36; 'sure': 0.36; 'but': 0.36; 'comment': 0.38; 'skip:b 20': 0.39; 'to:addr:python- list': 0.39; 'to:addr:python.org': 0.40; 'mar': 0.61; 'single': 0.61; 'name': 0.61; 'lists,': 0.63; 'more': 0.63; 'fill': 0.65; 'hope': 0.65; 'incorporate': 0.66; '2012': 0.69; 'idea.': 0.72; 'facilities': 0.76; 'gelonida': 0.84; 'subject:warning': 0.91; 'have.': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=pIjowLA7/lh9o52M99OYx5JIRmsXazpfw2XwYvYd6ZY=; b=G8FawfesTYx/cJ8cEJDuV/GOWmW5SGgQ3vntqlf8NU+Lua3CtFt4UQuu+RVjJ+0zCG gHrR0w7jxbdxab9DXZAoebD0Opw7Yd3g0z44AuYA+P6mMD4ZBrV1Q7NZAh2cIVG+atXY H7LarLDo3VOKVcz8tLJCQIh1U23RU/M77J2Xsi7OMjxvcMThQBuA3jYg5FEsFv++hcLW VcXE/EaOi3TLyzk1xv0r1KSJxIddTelMKUoOXbIcmzjDO2/fhp8cQ7rW9FcGbCGDv9E2 J9PrFS3u+xc2Zw4vHBSKczk2ompLFMmKb2pniMWu4wLfBc/Kv0boxcRBEtCSwcIUpkOI ee3A== MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 27 Mar 2012 08:50:35 +1100 Subject: Re: best way to create warning for obsolete functions and call new one From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332798645 news.xs4all.nl 6895 [2001:888:2000:d::a6]:37864 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22212 On Tue, Mar 27, 2012 at 7:26 AM, Gelonida N wrote: > One option I though of would be: > > def obsolete_func(func): > =A0 =A0def call_old(*args, **kwargs): > =A0 =A0 =A0 =A0print "func is old psl use new one" > =A0 =A0 =A0 =A0return func(*args, **kwargs) > =A0 =A0return call_old > > and > > def get_time(a=3D'high'): > =A0 return a + 'noon' That's a reasonable idea. Incorporate Dan's suggestion of using DeprecationWarning. You may want to try decorator syntax: def was(oldname): def _(func): globals()[oldname]=3Dfunc return func return _ @was("get_thyme") def get_time(a=3D'high'): return a + 'noon' That won't raise DeprecationWarning, though. It's a very simple assignment. The was() internal function could be enhanced to do a bit more work, but I'm not sure what version of Python you're using and what introspection facilities you have. But if you're happy with the old versions coming up with (*args,**kwargs) instead of their parameter lists, it's not difficult: def was(oldname): def _(func): def bounce(*args,**kwargs): # raise DeprecationWarning return func(*args,**kwargs) globals()[oldname]=3Dbounce return func return _ I've never actually used the Python warnings module, but any line of code you fill in at the comment will be executed any time the old name is used. In any case, it's still used with the same convenient decorator. You could even unify multiple functions under a single new name: @was("foo") @was("bar") def quux(spam,ham): return ham.eat() Hope that helps! ChrisA