Python: 学习系列之六:对象的内置函数
程序员文章站
2022-07-14 16:16:01
...
issubclass()与isinstance()
class ParentClass(object):
pass
class ChildClass(ParentClass):
pass
class ThirdClass(object):
pass
# issubclass判断类对象与类对象的关系
print(issubclass(ChildClass, ParentClass)) # True
print(issubclass(ChildClass, (ParentClass, ThirdClass))) # True,后面是个元组,只要满足元组内任意一个父类就为True
# isinstance判断实例对象与类对象的关系
c = ChildClass()
print(isinstance(c, ChildClass)) # True
print(isinstance(c, (ChildClass, ParentClass, ThirdClass))) # True,后面是个元组,只要满足元组内任意一个类就为True
type()
print(type(c)) #<class '__main__.ChildClass'>,获取指定对象的类型
print(type(ChildClass)) #<class 'type'>, 类对象的类型是type
上一篇: c++实现中值滤波
推荐阅读