Mainly Tech projects on Python and Electronic Design Automation.

Sunday, May 21, 2006

Function Attributes assigned by decorator

It appears to be straight forward to initialize function attributes by a decorator (the setup function).
The new function factory looks a lot like the class based version of my previous post, and performs similarly.

def make_standard_deviator2():
'''Generate functions that return running standard deviations
Uses function attributes applied by decorator

def setup(func):
func.N, func.X, func.X2 = [0.0]*3
return func

def sd(x):
'''returns standard deviation.
Uses function attributes holding running sums
from math import sqrt

sd.N += 1 # Num values
sd.X += x # sum values
sd.X2 += x**2 # sum squares

if sd.N<2: return 0.0
return sqrt((sd.N*sd.X2 - sd.X**2)

return sd

No comments:

Post a Comment


Subscribe Now: google

Add to Google Reader or Homepage

Go deh too!

Blog Archive