这是旧样式对象(从无从继承)的情况。这些对象不具有__class__
属性。我认为他们这样做是为了防止错误。用于Python 2.7实施例:
class A:
pass
class B(object):
pass
a = A()
b = B()
print(dir(a)) # ['__doc__', '__module__']
print(dir(b)) # ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
print(b.__class__) # <class '__main__.B'>
print(type(b)) # <class '__main__.B'>
#####################
# The intersting part
print(a.__class__) # __main__.A
print(type(a)) # <type 'instance'>
print(B.__class__) # <type 'type'>
print(type(B)) # <type 'type'>
print(type(A)) # <type 'classobj'>
#print(A.__class__) # AttributeError: class A has no attribute '__class__'
进一步信息请参阅本:
注:从CPython的给定的行被改变在2008上一次(commit),所以它似乎确实是一个兼容性的东西,或者他们只是忘了它。
可能的重复http://stackoverflow.com/questions/9610993/python-type-or-class-or-is – Kasramvd
@Kasramvd感谢您的链接,我相信它可能是http:// stackoverflow的重复。 COM /问题/ 1060499 /差间typeobj-和对象类。我不确定为什么他们在提问时没有出现,在提出问题时也没有出现在“提出的重复内容”中。 – MSeifert
是的,有时候会发生这种情况;) – Kasramvd