Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'exec': 0.07; 'roll': 0.07; 'decorator': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'func': 0.09; 'idle,': 0.09; 'message-id:@stoneleaf.us': 0.09; 'module)': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'def': 0.13; 'argument': 0.15; 'downside': 0.16; "function's": 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'tracebacks': 0.16; 'this:': 0.16; 'wrote:': 0.18; '2.x': 0.18; '(which': 0.19; "aren't": 0.21; 'header:In-Reply-To:1': 0.22; 'skip:{ 20': 0.23; 'somehow': 0.23; 'code': 0.25; 'skip:_ 20': 0.26; 'testing': 0.26; 'function': 0.27; 'do.': 0.28; 'looks': 0.29; 'clean.': 0.30; 'chris': 0.30; 'skip:- 70': 0.31; 'quite': 0.32; 'actual': 0.32; 'header:User- Agent:1': 0.33; 'rather': 0.33; 'instead': 0.33; 'to:addr:python- list': 0.34; 'see,': 0.34; 'list.': 0.35; 'something': 0.35; 'own,': 0.37; 'but': 0.37; 'useful': 0.38; 'help': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'received:websitewelcome.com': 0.64; 'piece': 0.66; 'received:184': 0.67; 'subject:!!!': 0.76; 'trim': 0.84 Date: Thu, 08 Dec 2011 15:34:13 -0800 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: python-list@python.org Subject: Re: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:4003 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323388282 news.xs4all.nl 6841 [2001:888:2000:d::a6]:47993 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16885 Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. If you want to roll your own, it looks something like this: 2.x code ---------------------------------------------------------------------- def trace(func, debugmode=debugmode): if debugmode: name = func.__name__ argspec = inspect.getargspec(func) signature = inspect.formatargspec( formatvalue=lambda val: "", *argspec)[1:-1] # trim parens new_func = ( 'def _wrapper_(%(signature)s):\n' ' print(">"+func.__name__)\n' ' result = func(%(signature)s)\n' ' print("<"+func.__name__)\n' ' return result\n' % {'signature':signature} ) evaldict = {'func':func} exec new_func in evaldict wrapped = evaldict['_wrapper_'] wrapped.__name__ = name wrapped.__doc__ = func.__doc__ wrapped.__module__ = func.__module__ wrapped.__dict__ = func.__dict__ wrapped.func_defaults = func.func_defaults return wrapped return func ---------------------------------------------------------------------- The downside (which you get even with Michele's decorator module) is that tracebacks aren't quite as clean. ~Ethan~