Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'wrapper': 0.07; 'email name:': 0.09; 'from:addr:gmx.de': 0.09; 'def': 0.13; 'wrote:': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; '*p,': 0.16; 'approach,': 0.16; 'closures': 0.16; 'self,': 0.16; 'subject:create': 0.16; 'received:(qmail invoked by alias)': 0.16; 'received:213.165.64': 0.16; 'object,': 0.19; 'received:gmx.net': 0.19; 'header:In-Reply-To:1': 0.22; 'mon,': 0.22; 'solving': 0.22; "didn't": 0.25; 'received:213.165': 0.25; 'object': 0.27; 'hey': 0.29; 'subject:?': 0.29; 'class': 0.29; 'wayne': 0.31; 'to:addr :python-list': 0.32; 'thank': 0.32; 'using': 0.34; 'question': 0.35; 'header:User-Agent:1': 0.35; '-0700,': 0.35; 'instances': 0.35; 'try:': 0.35; 'charset:us-ascii': 0.36; 'problems': 0.37; 'data': 0.37; 'self': 0.37; 'subject:with': 0.37; 'apr': 0.38; 'realize': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'received:de': 0.39; 'subject: (': 0.39; 'except': 0.39; 'you.': 0.61; 'give': 0.61; '2011': 0.62; 'yourself': 0.66; 'iii': 0.68; 'needing': 0.68 X-Authenticated: #18738959 X-Provags-ID: V01U2FsdGVkX1+lA982Kpmlo+wgx9rcU/JchHme0H3X6jluWBp1bV DKRBhCbwNOUtOi Date: Tue, 19 Apr 2011 09:12:13 +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: 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: 31 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303197185 news.xs4all.nl 41110 [::ffff:82.94.164.166]:60115 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3547 Hey Wayne, On Mon, Apr 18, 2011 at 04:04:15PM -0700, Wayne Witzel III wrote: > Going with the object approach, you could use Borg to give yourself the state between instances you mentioned. And since you are using an object, you'll have access to the data without needing to return it from the decorator. > > class StatefulDecorators(object): > _state = {} > def __new__(cls, *p, **k): > self = object.__new__(cls, *p, **k) > self.__dict__ = cls._state > return self > > def count_calls(self, function): > @functools.wraps(function) > def wrapper(*args, **kwargs): > try: > self.calls += 1 > except AttributeError: > self.calls = 1 > return function(*args, **kwargs) > return wrapper Brilliant! I didn't realize you could use member functions as decorators, too! That way, you can easily create closures to self, solving all the problems I was seeing. Just one question remains now: What is a "Borg" in this context? Thank you. Timo