Python检查类的实例 [英] Python check instances of classes

查看:79
本文介绍了Python检查类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检查对象是否是类的实例?不是具体类的实例,而是任何类的实例.

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.

推荐答案

isinstance()是您的朋友.它返回一个布尔值,并且可以通过以下方式用于检查类型.

isinstance() is your friend here. It returns a boolean and can be used in the following ways to check types.

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"

希望这会有所帮助.

这篇关于Python检查类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆