Skip to main content
 首页 » 编程设计

python之如何检查 Python 2.5 中的类相等性

2025年05月04日12bonelee

我查看了 Python 2.5 文档,但找不到这个问题的答案:How do I check if an object is the same class as another object?

def IsClass(obj1, obj2): 
     return obj1.class == obj2.class #doesn't work 

请您参考如下方法:

你可以使用

type(obj1) is type(obj2) 

请注意,您通常会尽量避免在 Python 中进行类型检查,而是依赖 duck typing .