Class Basics


i don’t get what wrong with the code??


class Triangle(object): 

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

    self.angle1 =ag1
    self.angle2 =ag2 
    self.angle3 =ag3

it gives this error

Did you create a class with the name Triangle?

<do not remove the three backticks above>


look at the error message in the console/output window, it will show you you have undefined (unnamed) variable (ag1, ag2 and ag3)


class Triangle(object):

def init(self,angle1,angle2,angle3):

why does it give error now ??


it shows indentation error
class Triangle(object):

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


it doesn’t mean you should just remove it

maybe check the hint? So you use the right variable


still the same error
class Triangle(object):

def __init__(self,angle1,angle2,angle3):
 self.angle1 =angle
 self.angle2 =angle 
 self.angle3 =angle


but angle is also an undefined variable?

maybe go to this exercise:

and read about what this code is suppose to do

in short, __init__() is called when we create a class instance, for example:

class Example(object):
    def __init__(self):
        print("i am being called")

# creating class instance
example = Example()

see? The init method gets called, even though we never explicitly call init. This is because this method is automatically called when creating an instance, if you remove example = Example() (which creates the instance) you see nothing will be printed

now, when we create a class instance, we can supply arguments:

# creating class instance
example = Example("i am the first argument!")

by adding the argument to self in the init method:

def __init__(self, first_argument):
    self.first_argument = first_argument

we can access the instance variable through self in other methods (which is something for later)


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