18. inheritance


#1

I’m naive, trying to understand the concepts @mtf : Even i have same doubt as @aspiring1 had. You had mentioned that “It is a class variable accessible only through the class directly” do you mean that we can access only with in class itself(Scope is within Class- which even i agree), If that is the case it should work in below format as well right

angle = 60
def init(self):
self.angle1 = angle
self.angle2 = angle
self.angle3 = angle

Could you please explain this.


Introduction to classes: 18. Inheritance exercise
#2

and we can access angle on instance of the class as well:

my_triangle = Equilateral()
print my_triangle.angle

no, within the class member variable like angle are automatically add to self, and that is how you access them, otherwise you have scope problem


#3
>>> class Triangle:
	def __init__(self, angle1, angle2, angle3):
		self.angle1 = angle1
		self.angle2 = angle2
		self.angle3 = angle3

		
>>> class Equilateral(Triangle):
	angle = 60
	def __init__(self):
		self.angle1 = self.angle
		self.angle2 = self.angle
		self.angle3 = self.angle

		
>>> Equilateral.angle
60
>>> instance = Equilateral()
>>> instance.angle
60
>>> 

As we can see, angle may be accessed directly from the class, or from the instance, in which case it is constant.


#4

@stetim94-- Thanks for mentioning the last point, I didn’t knew that.


#5

@mtf Thanks for info


#6

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