Python 如何检查(在运行时)一个类是否是另一个类的子类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4912972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check (at runtime) if one class is a subclass of another?
提问by snakile
Let's say that I have a class Suit and four subclasses of suit: Heart, Spade, Diamond, Club.
假设我有一个套装和四个子套装:Heart、Spade、Diamond、Club。
class Suit:
...
class Heart(Suit):
...
class Spade(Suit):
...
class Diamond(Suit):
...
class Club(Suit):
...
I have a method which receives a suit as a parameter, which is a class object, not an instance. More precisely, it may receive only one of the four values: Heart, Spade, Diamond, Club. How can I make an assertion which ensures such a thing? Something like:
我有一个方法将西装作为参数接收,它是一个类对象,而不是一个实例。更准确地说,它可能只接收以下四个值之一:Heart、Spade、Diamond、Club。我怎样才能做出保证这样事情的断言?就像是:
def my_method(suit):
assert(suit subclass of Suit)
...
I'm using Python 3.
我正在使用 Python 3。
采纳答案by Katriel
You can use issubclass()like this assert issubclass(suit, Suit).
您可以使用issubclass()像这样assert issubclass(suit, Suit)。
回答by Katriel
Excerpt:
摘抄:
Return true if
classis a subclass (direct, indirect or virtual) ofclassinfo.
如果
class是 的子类(直接、间接或虚拟), 则返回 trueclassinfo。
回答by David Heffernan
You can use isinstanceif you have an instance, or issubclassif you have a class. Normally thought its a bad idea. Normally in Python you work out if an object is capable of something by attempting to do that thing to it.
isinstance如果你有一个实例,或者issubclass你有一个类,你可以使用。通常认为这是一个坏主意。通常在 Python 中,您可以通过尝试对某个对象执行某项操作来确定该对象是否能够执行某项操作。
回答by XORcist
You can use the builtin issubclass. But type checking is usually seen as unneccessary because you can use duck-typing.
您可以使用内置的 issubclass。但是类型检查通常被认为是不必要的,因为您可以使用鸭子类型。
回答by Jameer Mulani
The issubclass(sub, sup)boolean function returns true if the given subclass subis indeed a subclass of the superclass sup.
该issubclass(sub, sup)如果给定的子类布尔函数返回真sub不愧是超类的子类sup。
回答by Jon Donnelly
Using issubclass seemed like a clean way to write loglevels. It kinda feels odd using it... but it seems cleaner than other options.
使用 issubclass 似乎是一种编写日志级别的干净方法。使用它感觉有点奇怪......但它似乎比其他选项更干净。
class Error(object): pass
class Warn(Error): pass
class Info(Warn): pass
class Debug(Info): pass
class Logger():
LEVEL = Info
@staticmethod
def log(text,level):
if issubclass(Logger.LEVEL,level):
print(text)
@staticmethod
def debug(text):
Logger.log(text,Debug)
@staticmethod
def info(text):
Logger.log(text,Info)
@staticmethod
def warn(text):
Logger.log(text,Warn)
@staticmethod
def error(text):
Logger.log(text,Error)
回答by Vigneshwaran Narayanan
#issubclass(child,parent)
class a:
pass
class b(a):
pass
class c(b):
pass
print(issubclass(c,b))#it returns true
回答by YaOzI
According to the Python doc, we can also use class.__mro__attribute or class.mro()method:
根据Python doc,我们还可以使用class.__mro__属性或class.mro()方法:
class Suit:
pass
class Heart(Suit):
pass
class Spade(Suit):
pass
class Diamond(Suit):
pass
class Club(Suit):
pass
>>> Heart.mro()
[<class '__main__.Heart'>, <class '__main__.Suit'>, <class 'object'>]
>>> Heart.__mro__
(<class '__main__.Heart'>, <class '__main__.Suit'>, <class 'object'>)
Suit in Heart.mro() # True
object in Heart.__mro__ # True
Spade in Heart.mro() # False

