Class it up! Why self.angle1 instead of angle1?


#1

Hi there!

here’s the code:

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

Here’s the error message:

In your check_angles method, make sure to check the sum of self.angle1 + self.angle2 + self.angle3

Why that?

Thank you in advance.

By the way: By choosing the category, how can I type something equivalent of the square?


#2

that’s like putting the value 5 in a list and expecting a global variable to now also refer to 5

[5]
print x  # probably doesn't refer to 5

#3

Sorry but I didn’t get it :no_mouth:


#4

scope, lets do an example:

def example(x):
   print x

example(5)
print x # error

x has a local scope, it only exists within the example function.

same problem with your class, the angles have a local scope within the init method, so we add them to self (refers to current instance of class), so we can access instance variable in other methods


#5

It’s not truly scope though, it can’t ever be accessed as just its name, it’s an attribute of the object, like a dictionary (and a dictionary is also the default method of storing those attributes)


#6

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