简体   繁体   中英

What is the issubclass equivalent of isinstance in python?

Given an object, how do I tell if it's a class, and a subclass of a given class Foo?

eg

class Bar(Foo):
  pass

isinstance(Bar(), Foo) # => True

issubclass(Bar, Foo) # <--- how do I do that?

It works exactly as one would expect it to work...

class Foo():
    pass

class Bar(Foo):
    pass

class Bar2():
    pass

print issubclass(Bar, Foo)  # True
print issubclass(Bar2, Foo) # False

If you want to know if an instance of a class derived from a given base class, you could use:

bar_instance = Bar()
print issubclass(bar_instance.__class__, Foo)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM