Reputation: 90
Why is is python throwing error at the second method in of this class?
class Lutany:
formulaa = 0
formulab = 0
def __init__(self,num):
self.num = num
self.formulaa = self.formulaA()
self.formulab = self.formulaB
def formulaA(self):
q = 0
num = self.num
while num > 0 :
q += num + (num - 1)
num = num - 1
return q
self.formulab = formulaB()
def formulaB(self):
num = self.num
q = 0
while num > 0 :
q = q + (num * num)
num = num - 1
return (0.5 * q)
if(__name__ == '__main__'):
l = Lutany(675)
p = l.formulaa
q = l.formulab
print " FormunlA returned " , str(p) , " for 675 "
print " FormulaB returned " , str(q) , " for 675 "
When running I have the following error:
~$ python lutany.py
Traceback (most recent call last):
File "lutany.py", line 30, in <module>
l = Lutany(675)
File "lutany.py", line 7, in init
self.formulab = self.formulaB
AttributeError: Lutany instance has no attribute 'formulaB'
Upvotes: 0
Views: 70
Reputation: 18921
You're missing parentheses here: self.formulab = self.formulaB
. Should be self.formulab = self.formulaB()
.
Upvotes: 0
Reputation: 5919
When you say, in formalaA self.formulab = formulaB()
did you mean self.formulab = self.formulaB()
?
Upvotes: 0
Reputation: 799180
You've indented the block for the formulaB()
method too much (although the edit to the question has destroyed evidence of this). Make sure that it is at the indent level directly beneath the class indent, not that of the previous method.
Upvotes: 2