Class it up


hey i sure made this code but i had to use help for it
1.i cannot understand why the check angles method takes only self as input
2.moreover the self.angle1=angle1 should have made angle1 to be referred directly but why did we have to use self.angle1 again??

i am not able to understand this class part sorry if it is obvious but i am not able to see it how

 def __init__(self,angle1,angle2,angle3):
    def check_angles(self):
        if self.angle1+self.angle2+self.angle3==180:
            return True
        return False


self is an another object. By doing this:


we make new variables (angle1) which we give a value of angle1.

now that we can access all instance variables (angle1, angle2 and angle3) through self object, we only have to supply self as parameter for check angles

To show how this work, we could do this:

 def __init__(self,angle1,angle2,angle3):
    def check_angles(self):
        if self.angleA+self.angleB+self.angleC==180:
            return True
        return False


No worries, classes can be a confusing concept. The __init__ method is known as a constructor which 1) defines the way that the class may be instantiated, and 2) is run once when the class is instantiated (created).

self refers to the class instance itself. Some languages opt to use the keyword this for that purpose. In some other languages, the presence of this is implied for methods within a class. In Java, for example, everything is an object and therefore this' presence can be implied when working within almost any situation.

I digress. Python's requirement to include self as the first argument to a method is a design choice of the language.

Anyway, looking at your code your __init__ method takes in the argument list self, angle1, angle2, and angle3.

self you already know about. angle1, angle2, and angle3 are arbitrary variables but by their names we can assume they are going to be three distinct numbers. The body of the constructor method (__init__) performs setting properties on the class itself (remember self refers to the current object).

Finally, the check_angles method takes only one method argument (self!) which you know about. Its body simply performs a quick check to see if the sum of all the angles equals 180 which is a geometry concept that defines a triangle. If so, you return True, otherwise False.

Hopefully this helped you!

Extra reading: Why explicit self has to stay


but when i use the same self.angle1 can't i use angle1 to refer it instead?
what is going around here?
or in your example didnt we make angle1=self.angleA??
why use whole statement?


okay and how will i be able to put values as input?
i thought this would be it but it just returns none..that means the compiler didn't even go to the check_angle method?


because self is another object, we make variable accessible through self object.

lets look at your class instance:


we want to be able to access the values (60) in other methods, so we use another class (self), now all instance variable can be accessed through self class, if we didn't, we would have to supply the values every-time:

class Triangle(object):
    """Make Triangles!"""
    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 self.angle1 + self.angle2 + self.angle3 == 180:
            return True
            return False
print a.check_angles(30,20,130)

which is not very useful.

this also answers your other question, in other to execute check_angles method, you need to call it, just like a regular function


what is the role of self here? i could have simply made 3 variables angle1,angle2,angle3..used them as attributes and given them values such as
and then later on checked the sum for them
if x+y+z==180?
what help exactly is this self object giving?

i get it self is like naming the object...we can call it objectA as well
but why do we put objectA.x while we can simply use x


you can't do this, this is not possible. You would have a scope problem, x, y and z would be local variable, which only exist inside the init method.

This is why we use another object. What is object is named, doesn't matter. self is just a naming convention


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