Python---issubclassとisinstance

4044 ワード

issubclassは、あるクラスが別のクラスのサブクラスであるかどうかを判断するために使用され、isinstanceは、あるオブジェクトがクラスであるかどうかを判断するためのインスタンスです.
 
 1 import math

 2 

 3 class Point:  

 4     def __init__(self, xValue, yValue):  

 5         self.X = xValue  

 6         self.Y = yValue

 7 

 8 class Circle(Point):  

 9     def __init__(self, xValue, yValue, rValue):  

10         Point.__init__(self, xValue, yValue)  

11         self.Radious = rValue  

12   

13     def area(self):  

14         return math.pi * self.Radious ** 2

15 

16 print("Point bases:", Point.__bases__)  

17 print("Circle bases:", Circle.__bases__)  

18   

19 print("Circle is the subclass of Point:", issubclass(Circle, Point))  

20 print("Point is the subclass of Circle:", issubclass(Point, Circle))

21 

22 

23 point = Point(3, 4)  

24 circle = Circle(4, 5, 2) 

25 print("point is an instace of Point:", isinstance(point, Point))  

26 print("circle is an instace of Point:", isinstance(circle, Point))  

27