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

Timo Schmiade the_isz at
Tue Apr 19 09:03:59 CEST 2011

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

> 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,


More information about the Python-list mailing list