python 有人可以解释一下这个错误到底是什么意思,TypeError: issubclass() arg 1 must be a class
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2464568/
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
Can someone explain what exactly this error means,TypeError: issubclass() arg 1 must be a class
提问by gizgok
I have zero idea as to why I'm getting this error.
我对为什么会收到此错误一无所知。
回答by Adrien Plisson
as people said, the 2 arguments of issubclass()
should be classes, not instances of an object.
正如人们所说, 的 2 个参数issubclass()
应该是类,而不是对象的实例。
consider this sample:
考虑这个样本:
>>> issubclass( 1, int )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 1 must be a class
>>> issubclass( type(1), int )
True
>>> isinstance( 1, int )
True
the key is the use of the type()
function to get the type of an instance for use with the issubclass()
function, which, as noted in another comment, is equivalent to calling isinstance()
关键是使用type()
函数来获取与函数一起使用的实例的类型issubclass()
,正如另一条评论中所指出的,这相当于调用isinstance()
回答by Felix Kling
It means that you don't provide a class as argument for issubclass()
. Both arguments have to be classes. Second argument can also be a tuple of classes.
这意味着您没有提供类作为issubclass()
. 两个参数都必须是类。第二个参数也可以是一个类元组。
If you show the code that throws this error, we can help further.
如果您显示引发此错误的代码,我们可以提供进一步帮助。
From the documentation:
从文档:
issubclass(class, classinfo)
Returntrue
ifclass
is a subclass (direct or indirect) ofclassinfo
. A class is considered a subclass of itself.classinfo
may be a tuple of class objects, in which case every entry inclassinfo
will be checked. In any other case, aTypeError
exception is raised.
issubclass(class, classinfo)
返回true
ifclass
是 的子类(直接或间接)classinfo
。一个类被认为是它自己的一个子类。classinfo
可能是类对象的元组,在这种情况下,classinfo
将检查每个条目。在任何其他情况下,TypeError
都会引发异常。
回答by James Sumners
The first argument to issubclass()
needs to be of type "class".
的第一个参数issubclass()
需要是“类”类型。
回答by inspectorG4dget
Basically this method tells you if the first parameter is a subclass of the second. So naturally, both your parameters need to be classes. It appears from your call, that you have called issubclass
without any parameters, which confuses the interpreter.
基本上这个方法会告诉你第一个参数是否是第二个参数的子类。因此,很自然地,您的两个参数都必须是类。从您的调用看来,您调用时issubclass
没有任何参数,这让解释器感到困惑。
Calling issubclass
is like asking the interpreter: "Hey! is this class a subclass of this other class?". However, since you have not provided two classes, you have essentially asked the interpretter: "Hey! I'm not going to show you anything, but tell me if this it's a subclass". This confuses the interpreter and that's why you get this error.
调用issubclass
就像问解释器:“嘿!这个类是另一个类的子类吗?”。但是,由于您没有提供两个类,您实际上是在问解释器:“嘿!我不会向您展示任何东西,但请告诉我这是否是一个子类”。这会使解释器感到困惑,这就是您收到此错误的原因。