Matt Hamilton-Glover
Matt Hamilton-Glover

Reputation: 21

Python AttributeError when running nosetest

I have a file called Model.py that contains the code

class ModelCalibrator():
    def __init__(self):
        self.file1 = 'Mortality_Population.txt'
        self.file2 = 'Deaths_1x1_adj.txt'
        self.MaxAge = 101
        self.MinAge = 18
        self.basisAges = np.array([18, 50, 100])[np.newaxis]
        self.mortalityData = PopulationData()
        self.deathRateData = DeathRateData()
        (self.age, self.phis) = computeBasisFunctions(ModelCalibrator)

def computeBasisFunctions(mc):
    MaxAge = mc.MaxAge
    MinAge = mc.MinAge
    age = np.arange(MinAge, MaxAge)[np.newaxis]

    basisAges = mc.basisAges
#calculations
...
return (age, phis)

In a separate test.py file I am running nosetests using the code

def testMC():
    data = ModelCalibrator()
    Phi = data.phis()
    assert_equal(Phi[0], 1)

This keeps telling me that I have an attributeerror: type object 'ModelCalibrator' has no attributes 'MaxAge'. Can anyone tell me where I am going wrong please?

Upvotes: 0

Views: 86

Answers (1)

meyer9
meyer9

Reputation: 1140

On this line, you are passing in the class instead of the object. Try replacing ModelCalibrator with self. The class is only a template for the object. self represents the current object with all of the properties set.

(self.age, self.phis) = computeBasisFunctions(self)

Alternatively, if you want these to be accessible without an object, you could set MaxAge and MinAge as class variables by moving them outside of the __init__ function, but inside the class as shown here.

Upvotes: 1

Related Questions