Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3545

Re: How to create a (transparent) decorator with status information?

Date 2011-04-19 09:03 +0200
From Timo Schmiade <the_isz@gmx.de>
Subject Re: How to create a (transparent) decorator with status information?
References <20110418124702.GA4046@TS2.igi.local> <BANLkTimu0j-L4YAof--Her_0vB3zGg4cJA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.551.1303196696.9059.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar


Thread

Re: How to create a (transparent) decorator with status information? Timo Schmiade <the_isz@gmx.de> - 2011-04-19 09:03 +0200

csiph-web