>>> class A: pass >>> class B(A): pass >>> issubclass(B, A) #A是否是B的子类 True >>> issubclass(B, B) True >>> issubclass(B, object) True >>> class C: pass >>> issubclass(B, C) False >>> b1 = B() >>> isinstance(b1, B) #123 True >>> isinstance(b1, A) True >>> isinstance(b1,C) False >>> isinstance(b1,(B,A,C)) True
issubclass(class, classinfo)
1.一个类被认为是自身的子类
2.classinfo可以是类对象组成的元组,只要class是其中一个候选类的子类,则返回True
isinstance(object, classinfo)
1.如果第一个参数不是对象,则永远返回False
2.如果第二个参数不是类或者由类对象组成的元组,会有TypeError异常
hasattr(object, name)
1.检查object对象是否含有该‘’name‘’属性
>>> class C: def __init__(self, x=0): self.x = x >>> c1 = C() >>> hasattr(c1,'x') True
getattr(object, name[, fault])
1.返回对象指定的属性值,如果不存在则会返回fault指定的错误
>>> getattr(c1, 'x') 0 >>> getattr(c1, 'y') Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> getattr(c1, 'y') AttributeError: 'C' object has no attribute 'y' >>> getattr(c1,'y',"您访问的属性不存在...") '您访问的属性不存在...'
setattr(object, name, value)
1.访问是否有该属性,若没有,则添加这个属性及其值
delattr(object, name)
1.删除指定的属性
property(fget=None, fset=None, fdel=None, doc=None)
1.通过属性设置属性
>>> class C: def __init__(self, size=10): self.size = size def getSize(self): return self.size def setSize(self, value): self.size = value def delSize(self): del self.size x = property(getSize,setSize,delSize) >>> c1 = C() >>> c1.getSize() 10 >>> c1.x 10 >>> c1.x = 18 >>> c1.x 18 >>> del c1.x
2.相当于重新封装3个属性