Python 检查类的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14549405/
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
Python check instances of classes
提问by exbluesbreaker
Is there any way to check if object is an instance of a class? Not an instance of a concrete class, but an instance of any class.
有没有办法检查对象是否是类的实例?不是具体类的实例,而是任何类的实例。
I can check that an object is not a class, not a module, not a traceback etc., but I am interested in a simple solution.
我可以检查一个对象不是一个类,不是一个模块,不是一个回溯等,但我对一个简单的解决方案感兴趣。
回答by Ber
Have you tried isinstance()built in function?
您是否尝试过isinstance()内置功能?
You could also look at hasattr(obj, '__class__')to see if the object was instantiated from some class type.
您还hasattr(obj, '__class__')可以查看对象是否从某个类类型实例化。
回答by Ned Batchelder
It's a bit hard to tell what you want, but perhaps inspect.isclass(val)is what you are looking for?
很难说出你想要什么,但也许这inspect.isclass(val)就是你正在寻找的?
回答by Matt Alcock
isinstance()is your friend here. It returns a boolean and can be used in the following ways to check types.
isinstance()是你的朋友吗?它返回一个布尔值,可以通过以下方式用于检查类型。
if isinstance(obj, (int, long, float, complex)):
print obj, "is a built-in number type"
if isinstance(obj, MyClass):
print obj, "is of type MyClass"
Hope this helps.
希望这可以帮助。
回答by user2682863
class test(object): pass
type(test)
returns
返回
<type 'type'>
instance = test()
type(instance)
returns
返回
<class '__main__.test'>
So that's one way to tell them apart.
所以这是区分它们的一种方法。
def is_instance(obj):
import inspect, types
if not hasattr(obj, '__dict__'):
return False
if inspect.isroutine(obj):
return False
if type(obj) == types.TypeType: # alternatively inspect.isclass(obj)
# class type
return False
else:
return True
回答by ellethee
I'm late. anyway think this should work.
我迟到了。无论如何认为这应该有效。
is_class = hasattr(obj, '__name__')
回答by Farsheed
or
或者
import inspect
inspect.isclass(myclass)
回答by f.rodrigues
Here's a dirt trick.
这是一个肮脏的把戏。
if str(type(this_object)) == "<type 'instance'>":
print "yes it is"
else:
print "no it isn't"
回答by Ben Hoff
Had to deal with something similar recently.
最近不得不处理类似的事情。
import inspect
class A:
pass
def func():
pass
instance_a = A()
def is_object(obj):
return inspect.isclass(type(obj)) and not type(obj) == type
is_object(A) # False
is_object(func) # False
is_object(instance_a) # True
回答by N.C. van Gilse
I had a similar problem at this turned out to work for me:
我有一个类似的问题,结果证明对我有用:
def isclass(obj):
return isinstance(obj, type)
回答by Rohit
I think i am pretty late on this one, and infact was struggling with same issue. So here is what worked for me.
我想我在这个问题上已经很晚了,事实上我也在为同样的问题苦苦挣扎。所以这对我有用。
>>> class A:
... pass
...
>>> obj = A()
>>> hasattr(obj, '__dict__')
True
>>> hasattr((1,2), '__dict__')
False
>>> hasattr(['a', 'b', 1], '__dict__')
False
>>> hasattr({'a':1, 'b':2}, '__dict__')
False
>>> hasattr({'a', 'b'}, '__dict__')
False
>>> hasattr(2, '__dict__')
False
>>> hasattr('hello', '__dict__')
False
>>> hasattr(2.5, '__dict__')
False
>>>
I tested this on both python 3 and 2.7.
我在 python 3 和 2.7 上对此进行了测试。

