nameerror - calling a separate method in an another one in Python -
i have multiple methods, don't know how put in python 3.4. eventually, library of methods , wrapping them in 1 class estimators. here have far:
class estimators(): """some methods building basic estimators. methods. """ def average(series): return float(sum(series))/len(series) def moving_average(series, n): return self.average(series[-n:]) def main(): series = [3, 10, 12, 13, 12, 10, 12] done = average(series) print(done) outputmv = moving_averge(series, 2) print('this moving average:\n %d.2' % outputmv)
it throws
`nameerror: global name 'average' not defined
also, want make sure right architecture. practice in python define multiple methods have (functional) dependencies along? functional dependencies mean methods might use methods defined outside scope.
in case, seems better use module rather class. module separate python file. if name file estimators.py , put functions in them, can import them file so:
from estimators import average series = [3, 10, 12, 13, 12, 10, 12] done = average(series)
edit respond comment:
if estimators.py file looks this, should import moving average function:
def average(series): return float(sum(series))/len(series) def moving_average(series, n): return average(series[-n:])
notice changed self.average
average
, since self
refers instance of class, , these functions not in class.
Comments
Post a Comment