18/18 Some questions about classes


Hello, i did not understand some basics about classes and i appreciate your help.
Working code :

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

print my_triangle.number_of_sides
print my_triangle.check_angles()

class Equilateral(Triangle):
  angle = 60
  def __init__(self):
    self.angle1 = self.angle
    self.angle2 = self.angle
    self.angle3 = self.angle

1- In the __unit__ of the base class, we needed to give it 4 arguments but when we used the Equilateral class and inherited from the triangle __unit__ took only 1 argument. I didnt understand why.
2-I didnt understand why we used (self.angle1= self.angle) but not (self.angle1 = angle)
3-print my_triangle.check_angles() why can this work while there is no argument in the check_angles, and is this equal to check_angles(my_triangle) ?


my previous reply was a bit of a mess, lets do it better.

first lets get some terms right:

class Triangle(object):
  number_of_sides = 3 # member variable, same for all instances
  def __init__(self, angle1, angle2, angle3):
    # defining instance variable angle1, give it a value angle1 parameter
    self.angle1 = angle1 
    # similair to angle1, but now for angle2
    self.angle2 = angle2
    self.angle3 = angle3

# creating instance my_triangle
my_triangle = Triangle(30, 60, 90)

__init__() is a magic method, the magic part is that __init__() automatically gets called when creating an instance. Normally, for a method (or a function for that matter) to execute, it needs to be called, not with __init__, as i said, this method gets called when creating an instance of class.

at init, we create variable which are unique for an instance. For example if we created another instance:

my_triangle = Triangle(30, 60, 90)
second_triangle = Triangle(45, 55, 80)

the angles are different. However, the number of sides is the same (3), which is why we use a member variable for this (member variable have the same value for all instances)

to access instance variable and member variable in a method we need to do self.instanceName or self.memberName

instance variable we have to add manually to self, this is what we do in __init__

member variables are automatically added to self, which we can prove:

class Triangle(object):
  number_of_sides = 3
  def prove_of_concept(self):
      print self.number_of_sides

my_triangle = Triangle()

because angle would be an undefined variable. To access member variables, we need self, as you have hopefully seen

given in __init__() we created instance variables, now we can access this instance variable through self, so we don’t have to provide arguments when calling check_angles.

Why would we provide angles for check_angle method? Our triangle already has the angles, so we can just use them, introducing new angles might in fact cause bugs and problems in our program


So far, we ignored one question: what is self? It refers to the current instance of class, let me show you:

class Example(object):
    def __init__(self, x):
        self.x = x

    def print_x(self):
         print self.x

instance_one = Example(5)
instance_two = Example(2)

instance_one.print_x # 5
instance_two.print_x # 2

so self keeps track of the instance we call the method on, that is need, which again we can prove:


now we call the method on the class, and we have to provide the instance. Now we actually pass a value to self, and it seems that when you call a method on a instance, python handles passing self, isn’t that neat?


so does this mean that
Class.method(instance) is equal to instance.method()


yes, but unless you have a really good reason not to, instance.method() should be preferred. It was just to show how this all works.


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