Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我浏览了 Python 2.5 文档,但找不到答案:如何检查一个对象是否与另一个对象是同一个类?
def IsClass(obj1, obj2): return obj1.class == obj2.class #doesn't work
您可以使用
type(obj1) is type(obj2)
请注意,您通常会尽量避免在 Python 中进行类型检查,而是依赖鸭子类型。
我认为您想要做的是使用类型(obj)。:)
-编辑- 看起来他打败了我。他对 Duck Typing 的看法是正确的。