# Class It Up: Using angle1 instead of self.angle1

#1

Hi all! I've gotten past this exercise already, but I'd like to know one thing. The following code appears to work:

``````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):
if self.angle1 + self.angle2 + self.angle3 == 180:
return True
else: return False``````

However, what I'd like to know is why replacing self.angle1 in the check_angles method doesn't work. The code should look something like this:

``````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):
if angle1 + angle2 + angle3 == 180:
return True
else: return False``````

However, this fails to work. Since self.angle1 = angle1, shouldn't we be able to replace all self.angle1 with angle1? Thanks for your help!

#2

``def __init__(self, angle1, angle2, angle3):``

it has angle1, angle2 and angle3 has parameters. Then we make new self.angle variables, so that we can access angle of class instance through self

self makes variable accessible in other methods.

What you want to do would require:

`````` 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, angle1, angle2, angle3):
if angle1 + angle2 + angle3 == 180:
return True
else: return False``````

but then, you would have to supply the angles, everytime you want to call the method:

``````T = Triangle(30,20,130)
print T.check_angles(30,20,130)``````

not very handy

#3

I see. Thanks for your help!

#4

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