如何检查Python 2.5中的类相等? [英] How to check class equality in Python 2.5?

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

问题描述

我看过Python 2.5文档,我找不到答案:如何检查一个对象是否是另一个对象的同一个类?

  def IsClass(obj1,obj2):
return obj1.class == obj2.class#does not work
pre>

解决方案

您可以使用

  type(obj1)is type(obj2)

请注意,您通常会尝试避免类型检查而是依赖于鸭式打字


I've looked through Python 2.5 documentation and I couldn't find an answer to this: 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

解决方案

You can use

type(obj1) is type(obj2)

Note that you usually try to avoid type checking in Python, but rather rely on duck typing.

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

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