Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3547
| Date | 2011-04-19 09:12 +0200 |
|---|---|
| From | Timo Schmiade <the_isz@gmx.de> |
| Subject | Re: How to create a (transparent) decorator with status information? |
| References | <mailman.510.1303130820.9059.python-list@python.org> <a61d0b91-a826-4530-b46e-be01d5ed0856@glegroupsg2000goo.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.553.1303197185.9059.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Re: How to create a (transparent) decorator with status information? Wayne Witzel III <wwitzel3@gmail.com> - 2011-04-18 16:04 -0700 Re: How to create a (transparent) decorator with status information? Timo Schmiade <the_isz@gmx.de> - 2011-04-19 09:12 +0200 Re: How to create a (transparent) decorator with status information? Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-19 08:20 -0600 Re: How to create a (transparent) decorator with status information? Timo Schmiade <the_isz@gmx.de> - 2011-04-19 17:12 +0200
csiph-web