16. I am unsure about the code


It says that this code is correct, but I am unsure. Shouldn't I use self.angle1... in check_angles function, at least that's what others have done. They have also used only one argument under check_angles function and that is self. Why is that ok?

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


what you do is technically not wrong, it is just not very neat. thanks to __init__() the angles are available through self, so then you could do this:

def check_angles(self):
   if self.angle1 ...

this method has another advantage, if you create an object instance:

triangle = Triangle(60,60,60)

you can just call the method:

print triangle.heck_angles()

with your method, you would have to provide the angles again as arguments when calling the method:

print triangle.heck_angles(60,60,60)

plus, you could then change the angles, that feels a bit strange


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.