Introduction to Classes_Instantiate an Object


#1

Error:
Traceback (most recent call last):
File "python", line 13, in
TypeError: init() takes exactly 4 arguments (5 given)

class Triangle(object):
def init(self,angle1,angle2,angle3):
angle1.self=angle1
angle2.self=angle2
angle3.self=angle3
number_of_sides=3
def check_angles(self):
if angle1.self+angle2.self+angle3.self==180:
return True
else:
return False

my_triangle=Triangle('my_triangle',90,30,60)
print my_triangle.number_of_sides
print my_triangle.check_angles(self,90,30,60)


#2

here:

my_triangle=Triangle('my_triangle',90,30,60)

why 'my_triangle'? you store the instance in my_triangle variable, this i understand, but you don't need to provide a string as first arguments, your init parameters are not designed to handle this


#3

I removed 'my_triangle'.
Still error.

Traceback (most recent call last):
File "python", line 13, in
File "python", line 3, in init
AttributeError: 'int' object has no attribute 'self'

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

my_triangle=Triangle(90,30,60)
print my_triangle.number_of_sides() 
print my_triangle.check_angles(self,90,30,60)

#4

self refers to the current instance of a class. So, what we want to do in __init__() is create new variable accessible through self. So we can use this newly created variable in other methods for specific instances.

the syntax for this:

self.variable_name = "value"`

in your case, you want to create angle1, angle2 and angle3:

self.angle1 = angle1

so now self.angle1 contains the value of angle1 (which you can determine when you create an instance)

so now, we can use self.angle1 in check_angles method


#5

Error:
Traceback (most recent call last):
File "python", line 15, in
TypeError: 'int' object is not callable

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

my_triangle=Triangle(90,30,60)
print my_triangle.number_of_sides()
print my_triangle.check_angles(self,90,30,60)


#6

Traceback (most recent call last):
File "python", line 15, in
TypeError: 'int' object is not callable

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
    else:
        return False

my_triangle=Triangle(90,30,60)
print my_triangle.number_of_sides()
print my_triangle.check_angles(self,90,30,60)


#7

here:

print my_triangle.number_of_sides()

number_of_sides is a member variable (its the same for all class instances), to access a member variable, we have the following syntax:

instance_name.member_variable

no parentheses at the end, that is for methods (check_angles is a method)


#8

Traceback (most recent call last):
File "python", line 16, in
TypeError: check_angles() takes exactly 1 argument (2 given)

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
    else:
        return False

my_triangle=Triangle(90,30,60)
print my_triangle.number_of_sides
print my_triangle.check_angles('my_triangle')


#9

we talked about self, it refers to the current class instance, so here:

print my_triangle.check_angles('my_triangle')

you don't need to provide string argument, given the above reason i just mentioned

we talked about this when you had trouble creating class instance


#10

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