Python -----issubclass和isinstance
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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。