type|isinstance|issubclass

类型判断

type()不会认为子类是一种父类类型
isinstance()会认为子类是一种父类类型

class Foo(object):
    pass
 
class Bar(Foo):
    pass
 
print(type(Foo()) == Foo)

True

print(type(Bar()) == Foo)

False

# isinstance参数为对象和类
print(isinstance(Bar(),Foo))

True

issubclass

class Parent:
    pass

class Sub(Parent):
    pass

print(issubclass(Sub, Parent))

True

print(issubclass(Parent, object))

True```

你可能感兴趣的:(python,开发语言,python)