Re: Generators and Decorators doing my head in ..

2005-09-07 Thread Michele Simionato
I usually point out my decorator module (http://www.phyast.pitt.edu/~micheles/python/decorator.zip) to simplify decorator usage. In this case you would use it as follows: from decorator import decorator @decorator # convert logFunctionCalls into a decorator def logFunctionCalls(function, *args, *

Re: Generators and Decorators doing my head in ..

2005-09-06 Thread simonvc
Fantastic, thanks Leif and Paul, My problem is that i thought the decorator worked at the function runtime, not when the function gets created. -- http://mail.python.org/mailman/listinfo/python-list

Re: Generators and Decorators doing my head in ..

2005-09-06 Thread bruno modulix
Paul McGuire wrote: (snip) > (This is a quick-and-dirty example, but it works. A proper iterator s/iterator/decorator/ (snip) -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailma

Re: Generators and Decorators doing my head in ..

2005-09-06 Thread Leif K-Brooks
[EMAIL PROTECTED] wrote: > Im trying to create a decorator that counts the number of times a > function is run. Your code doesn't work because decorators are run at function creation time, not at function run time. Try this instead: from itertools import count def logFunctionCalls(function):

Re: Generators and Decorators doing my head in ..

2005-09-06 Thread Paul McGuire
Compare this to your original: def logFunctionCalls(function): ec = FunctionCounter() def decoratedFunction(*args,**kwargs): print "Entering function:", function.__name__, ec.next() function(*args,**kwargs) return decoratedFunction @logFunctionCalls def doWork(): p

Generators and Decorators doing my head in ..

2005-09-06 Thread simonvc
Hi Can someone please help me understand this (i shouldn't have tried to learn decorators and generators at the same time..). Im trying to create a decorator that counts the number of times a function is run. Somthing like: def FunctionCounter(): n=0 while 1: yield n n=n+