Python 如何检查变量是否为类对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15841417/
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 to check a variable is class object or not
提问by pylover
Assume a simple class:
假设一个简单的类:
class MyClass(object):
pass
.
.
.
m = MyClass
print type(m) # gets: <type 'classobj'>
# if m is classobj, how can i check a variable is class object?
My question is: how can i check a variable is a class object?
我的问题是:如何检查变量是否是类对象?
a simple solution:
一个简单的解决方案:
if str(type(m)) == "<type 'classobj'>":
# do something
But i think there is at least one classic way to check that.
但我认为至少有一种经典的方法可以检查这一点。
采纳答案by abarnert
In 2.x, a class object can be be a type(new-style classes) or a classobj(classic classes). The typetype is a builtin, but the classobjtype is not. So, how do you get it? That's what the typesmodule is for.
在 2.x 中,类对象可以是 a type(新式类)或 a classobj(经典类)。该type类型是内置的,但classobj类型不是。那么,你如何得到它?这就是types模块的用途。
isinstance(MyClass, (types.TypeType, types.ClassType))
In 3.x, you don't need to worry about classic classes, so:
在 3.x 中,您无需担心经典类,因此:
isinstance(MyClass, type)
Even if you want to compare the types directly, you should never compare anyobjects by their str. Compare:
即使你想直接比较的类型,你永远不应该比任何通过他们的对象str。相比:
>>> class MyClassicClass:
... pass
>>> str(type(MyClassicClass)) == "<type 'classobj'>"
True
>>> str("<type 'classobj'>") == "<type 'classobj'>"
True
You can almost always just compare objects directly:
您几乎总是可以直接比较对象:
>>> type(MyClassicClass) == types.ClassType
True
>>> "<type 'classobj'>" == types.ClassType
False
(And in the incredibly rare cases where you really do need to compare the string representation for some reason, you want the repr, not the str.)
(并且在非常罕见的情况下,由于某种原因您确实需要比较字符串表示形式,您需要的是repr,而不是str。)
回答by Piotr Hajduga
Is this what you're looking for?
这是你要找的吗?
>>> class A(object):
... pass
>>> isinstance(A, type)
True
>>> isinstance("asd", type)
False
But works only for classes inherited from object.
但仅适用于从对象继承的类。
回答by kindall
A new-style class (derived explicitly from objectas in your example, or implicitly in Python 3) is not of type classobj. Only old-style classes are classobjs. So for your example class as written, you could simply check to see if MyClassis an instance of type, as all new-style classes are:
新式类(object在您的示例中显式派生自,或在 Python 3 中隐式派生)不是 type classobj。只有旧式的类是classobjs。因此,对于您编写的示例类,您可以简单地检查是否MyClass是 的实例type,因为所有新式类都是:
isinstance(MyClass, type)
Because all new-style classes are derived from objectyou can also do:
因为所有新式类都是从object你派生出来的,你也可以这样做:
issubclass(MyClass, object)
I would advise against:
我建议不要:
type(MyClass) == type
This will fail if the type of a class is not type, but a metaclass. Although there may be use cases for that, of course.
如果类的类型不是type,而是元类,这将失败。当然,尽管可能会有用例。
If you really need to see if you've got an old-style class, you can import the typesmodule and use types.ClassType, or just declare an old-style class and see if the class you're testing is of the same type:
如果您确实需要查看是否有旧式类,则可以导入types模块并使用types.ClassType,或者仅声明一个旧式类并查看您正在测试的类是否属于相同类型:
class OldStyleClass:
pass
OldStyleClass = type(OldStyleClass)
isinstance(MyClass, OldStyleClass)
回答by sergzach
Sometimes the next is working (if you want to determine how to print the object):
有时下一个工作(如果你想确定如何打印对象):
def is_class(o):
return hasattr(o, '__dict__')

