Class Basics


#1

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-classes/exercises/class-basics?action=lesson_resume

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

```python

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>

#2

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


#3

class Triangle(object):

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

why does it give error now ??


#4

it shows indentation error
class Triangle(object):

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

#5

it doesn’t mean you should just remove it

maybe check the hint? So you use the right variable


#6

still the same error
class Triangle(object):

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

#7

but angle is also an undefined variable?

maybe go to this exercise:

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-classes/exercises/more-on-init-and-self?action=lesson_resume

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)


#8

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