16.19 - Class it Up : doesn't return true

class Triangle(object):
    def __init__(self, angle1,angle2,angle3):
        Triangle.self = self
        Triangle.angle1 = angle1
        Triangle.angle2 = angle2
        Triangle.angle3 = angle3
    number_of_sides = 3
    def check_angles(self):
        if self.angle1+self.angle2+self.angle3 == 180:
            return True
            return False
#t1 = Triangle(30,60,90)
#print t1.check_angles()

It says the code won't return true for valid triangle angle values, but when I uncomment my last two lines and test it, I get True. Can someone tell me what I'm overlooking?


The issue is not with your method, but with the class constructor:

Triangle.self = self

The above line can go.

Triangle.angle1 = angle1

should read,

self.angle1 = angle1

and the same with the other two.


Oh! I see it! I never defined self.angle1 or the others. My grasp of this whole topic is still pretty tenuous.



self is the internal name of the instance object created by the constructor. When we run instance methods, the method refers to the self as the context object, the owner of the method.

isosceles = Triangle(75, 30, 75)
print isosceles.check_angles()     # True

When we call the method on the instance, that instance is self inside the method.