How to check class equality in Python 2.5?

余生颓废 提交于 2019-12-01 10:41:22

问题


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

回答1:


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.




回答2:


I think what you want to do is use type(obj). :)

-EDIT- Looks like he beat me to it. And he's right about the Duck Typing.



来源:https://stackoverflow.com/questions/5834829/how-to-check-class-equality-in-python-2-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!