首页 > 编程语言 > 详细

Python -----issubclass和isinstance

时间:2014-02-21 20:51:10      阅读:468      评论:0      收藏:0      [点我收藏+]

issubclass用于判断一个类是否为另一个类的子类,isinstance用于判断一个对象是否某类的一个实例

 

bubuko.com,布布扣
 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   
bubuko.com,布布扣

Python -----issubclass和isinstance

原文:http://www.cnblogs.com/hanwenhuazuibang/p/3558958.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!