Class It Up - Why is it working?


#1

The code is working, but I don't fully understand why, especially the piece number_of_sides = 3:

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

It looks like this:

Does it mean that the variable belongs to the class and not the function?


#2

Yes, it belongs to the class. Every instance has this variable.

    right = Triangle(30, 60, 90)
    print right.number_of_sides    # 3

#3

Thank you very much!


#4

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