问题:如何检查变量是否为类?
我想知道如何检查变量是否是类(不是实例!)。
我试图使用该函数isinstance(object, class_or_type_or_tuple)
来执行此操作,但我不知道类将具有哪种类型。
例如,在以下代码中
class Foo: pass
isinstance(Foo, **???**) # i want to make this return True.
我试图class
用???代替“ ” ,但我意识到这class
是python中的关键字。
I was wondering how to check whether a variable is a class (not an instance!) or not.
I’ve tried to use the function isinstance(object, class_or_type_or_tuple)
to do this, but I don’t know what type would a class will have.
For example, in the following code
class Foo: pass
isinstance(Foo, **???**) # i want to make this return True.
I tried to substitute “class
” with ???, but I realized that class
is a keyword in python.
回答 0
更好的是:使用该inspect.isclass
功能。
>>> import inspect
>>> class X(object):
... pass
...
>>> inspect.isclass(X)
True
>>> x = X()
>>> isinstance(x, X)
True
>>> y = 25
>>> isinstance(y, X)
False
Even better: use the inspect.isclass
function.
>>> import inspect
>>> class X(object):
... pass
...
>>> inspect.isclass(X)
True
>>> x = X()
>>> isinstance(x, X)
True
>>> y = 25
>>> isinstance(y, X)
False
回答 1
inspect.isclass可能是最好的解决方案,并且很容易看到它是如何实现的
def isclass(object):
"""Return true if the object is a class.
Class objects provide these attributes:
__doc__ documentation string
__module__ name of module in which this class was defined"""
return isinstance(object, (type, types.ClassType))
The inspect.isclass is probably the best solution, and it’s really easy to see how it’s actually implemented
def isclass(object):
"""Return true if the object is a class.
Class objects provide these attributes:
__doc__ documentation string
__module__ name of module in which this class was defined"""
return isinstance(object, (type, types.ClassType))
回答 2
>>> class X(object):
... pass
...
>>> type(X)
<type 'type'>
>>> isinstance(X,type)
True
>>> class X(object):
... pass
...
>>> type(X)
<type 'type'>
>>> isinstance(X,type)
True
回答 3
isinstance(X, type)
返回True
if X
是class,False
如果不是,则返回。
isinstance(X, type)
Return True
if X
is class and False
if not.
回答 4
此检查与Python 2.x和Python 3.x兼容。
import six
isinstance(obj, six.class_types)
这基本上是一个包装函数,执行与andrea_crotti答案中相同的检查。
例:
>>> import datetime
>>> isinstance(datetime.date, six.class_types)
>>> True
>>> isinstance(datetime.date.min, six.class_types)
>>> False
This check is compatible with both Python 2.x and Python 3.x.
import six
isinstance(obj, six.class_types)
This is basically a wrapper function that performs the same check as in andrea_crotti answer.
Example:
>>> import datetime
>>> isinstance(datetime.date, six.class_types)
>>> True
>>> isinstance(datetime.date.min, six.class_types)
>>> False
回答 5
回答 6
最简单的方法是使用inspect.isclass
投票最多的答案中的内容。
实现细节可以在python2 inspect和python3 inspect中找到。
对于新型类:isinstance(object, type)
对于老式类:isinstance(object, types.ClassType)
em,对于老式类,它正在使用types.ClassType
,这是来自types.py的代码:
class _C:
def _m(self): pass
ClassType = type(_C)
simplest way is to use inspect.isclass
as posted in the most-voted answer.
the implementation details could be found at python2 inspect and python3 inspect.
for new-style class: isinstance(object, type)
for old-style class: isinstance(object, types.ClassType)
em, for old-style class, it is using types.ClassType
, here is the code from types.py:
class _C:
def _m(self): pass
ClassType = type(_C)
回答 7
本杰明·彼得森(Benjamin Peterson)inspect.isclass()
对于这项工作的使用是正确的。但是请注意,您可以使用内置函数issubclass来测试Class
对象是否是特定对象Class
,因此是否是隐式对象。根据您的用例,这可能更像pythonic。Class
from typing import Type, Any
def isclass(cl: Type[Any]):
try:
return issubclass(cl, cl)
except TypeError:
return False
然后可以这样使用:
>>> class X():
... pass
...
>>> isclass(X)
True
>>> isclass(X())
False
Benjamin Peterson is correct about the use of inspect.isclass()
for this job.
But note that you can test if a Class
object is a specific Class
, and therefore implicitly a Class
, using the built-in function issubclass.
Depending on your use-case this can be more pythonic.
from typing import Type, Any
def isclass(cl: Type[Any]):
try:
return issubclass(cl, cl)
except TypeError:
return False
Can then be used like this:
>>> class X():
... pass
...
>>> isclass(X)
True
>>> isclass(X())
False
回答 8
这里已经有一些可行的解决方案,但这是另一个解决方案:
>>> import types
>>> class Dummy: pass
>>> type(Dummy) is types.ClassType
True
There are some working solutions here already, but here’s another one:
>>> import types
>>> class Dummy: pass
>>> type(Dummy) is types.ClassType
True