0

Error is:

Equilateral object has no attribute angle1.

please suggest how to fix this error and also please explain how self works. I am confused where to use self and where to not

 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 
class Equilateral(Triangle): //inheritance
 angle=60
 def __init__(self):
 self.angle=self.angle1
 self.angle=self.angle2
 self.angle=self.angle3
man=Equilateral()
man.check_angles()
Scott
6,4195 gold badges37 silver badges52 bronze badges
asked Jun 8, 2015 at 22:04
1
  • 1
    Helpful hint: Python comments use #, not //. Commented Jun 8, 2015 at 22:12

3 Answers 3

2

You have it the wrong way around

self.angle1= self.angle
etc

Self refers to the instantiated object, much like 'this' in java. You attach attributes to the object using this keyword.

When defining variables on an object, attributes at the beginning of your class definition do not need self- they are class attributes which all instances of the object will create on instantiation, whereas variables you change or set using self are instance variables and not found on all instances of the object.

answered Jun 8, 2015 at 22:06
Sign up to request clarification or add additional context in comments.

1 Comment

The other answers given about initialization are also correct (and are a better practice to stick by)
2

Different from other languages, Python does not call __init__() of the super class. You have to call it yourself:

class Equilateral(Triangle):
 angle=60
 def __init__(self, ...):
 super().__init__(...)
 self.angle=self.angle1

More details

answered Jun 8, 2015 at 22:10

Comments

2

You have to call __init__ from the super class:

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):
 return self.angle1+self.angle2+self.angle3==180:
class Equilateral(Triangle):
 angle=60
 def __init__(self):
 Triangle.__init__(self, self.angle, self.angle, self.angle)
man=Equilateral()
man.check_angles()
answered Jun 8, 2015 at 22:07

6 Comments

uhm.. what about calling it using super(Equilateral, self).__init__(...)
If angle (or angle1,... whatever) is an attribute of the superclass, he should just assign it there and there would be no need to call anything. It would be reached through self.
@Pynchia: What about other fields defined by the superclass? Or if something changes there? That is the reason why Python does not call it automatically, it leaves freedom to the developer.
I am not sure I get your point. Of course there are multiple ways to solve/design. I have the impression the approach hinted in the question may not be appropriate for the goal. If we get to know what the goal is.
calling by above method python throws an error-unbound method __init__() must be called with Triangle instance as first argument (got int instance instead)
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.