Decorators are your friends. You can wrap a function
and give it additional functionality. For instance just
yesterday I needed to keep track of how many times
a function is called. This can be done with this
decorator:
.def with_counter(f):
. def wrappedf(*args, **kw):
. wrappedf.counter += 1
. return f(*args, **kw)
. wrappedf.counter = 0
. wrappedf.func_name = f.func_name
. return wrappedf
[EMAIL PROTECTED] # requires Python 2.4
.def g():
. print "called"
.print g.counter
.g()
.print g.counter
This is intented just as an appetizer. Look at the
cookbook for more examples.
Michele Simionato
--
http://mail.python.org/mailman/listinfo/python-list