Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'instance': 0.05; 'wrapper': 0.07; 'email name:': 0.09; 'from:addr:gmx.de': 0.09; 'kelly': 0.09; 'object.': 0.09; 'def': 0.13; 'wrote:': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; 'first-class': 0.16; 'subject:create': 0.16; 'received:(qmail invoked by alias)': 0.16; 'received:213.165.64': 0.16; 'received:gmx.net': 0.19; 'header:In- Reply-To:1': 0.22; 'mon,': 0.22; 'objects,': 0.23; 'received:213.165': 0.25; "i'm": 0.26; 'object': 0.27; 'function': 0.27; 'hey': 0.29; 'subject:?': 0.29; 'probably': 0.30; 'decorators': 0.31; 'wayne': 0.31; 'to:addr:python-list': 0.32; 'thank': 0.32; 'store': 0.32; 'idea': 0.32; 'thinking': 0.33; 'with.': 0.33; 'header:User-Agent:1': 0.35; 'too': 0.36; 'case,': 0.36; 'charset:us-ascii': 0.36; 'should': 0.37; 'subject:with': 0.37; 'apr': 0.38; 'helped': 0.38; 'members': 0.38; 'to:addr:python.org': 0.39; 'received:de': 0.39; 'subject: (': 0.39; 'would': 0.40; 'simple': 0.60; 'give': 0.61; '2011': 0.62; 'ian,': 0.84; 'shared,': 0.84 X-Authenticated: #18738959 X-Provags-ID: V01U2FsdGVkX18D6Sds5HMDsRNKxZdk+UtsW5A33005/z9RKlxW4v 5BgQ8kE5FtYiKi Date: Tue, 19 Apr 2011 09:03:59 +0200 From: Timo Schmiade To: python-list@python.org Subject: Re: How to create a (transparent) decorator with status information? Mail-Followup-To: python-list@python.org References: <20110418124702.GA4046@TS2.igi.local> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Y-GMX-Trusted: 0 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: 26 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303196697 news.xs4all.nl 34849 [::ffff:82.94.164.166]:34931 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3545 Hey Ian, On Mon, Apr 18, 2011 at 01:07:58PM -0600, Ian Kelly wrote: > In the simple case, just store the state on the wrapper function itself: > > def call_counts(function): > @functools.wraps(function) > def wrapper(*args, **kwargs): > wrapper.num_calls += 1 > return function(*args, **kwargs) > wrapper.num_calls = 0 > return wrapper Of course! Functions are first-class objects, so I can give them members just as I would do with any other object. I'm still thinking too much C++... > If you want the state to be shared, you should probably store it in an > object and use an instance method as the decorator: Yes, this is the same idea that Wayne came up with. Thank you as well, that helped me understand decorators a lot more. Kind regards, Timo