2016-04-05 164 views

回答

3

是,在這兩個Python2和Python3:

​​

注意,在Python2的類,不屬於object子類是老式類。然而,這樣的類的實例仍然實例object

>>> class Foo: 
...  pass 
... 
>>> foo = Foo() 
>>> foo 
<__main__.Foo instance at 0x7f2a33474bd8> 
>>> type(foo) 
<type 'instance'> 
>>> foo.__class__ 
<class __main__.Foo at 0x7f2a33468668> 
>>> Foo.mro() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: class Foo has no attribute 'mro' 
>>> issubclass(Foo, object) 
False 
>>> isinstance(foo, object) 
True 
>>> type(foo).mro() 
[<type 'instance'>, <type 'object'>] 
>>> issubclass(type(foo), object) 
True 

編輯:我懷疑一些事情可能爲Python <是不同的2.6,並可能在C.

實施
1

是。

isinstance函數可以告訴你這個。

>>> isinstance(None, object) 
True 
+2

從技術上講,實例檢查與子類檢查不同,它與mro檢查也不是同一回事。 – o11c

+0

@ o11c'str .__ bases__'給出'object'。但'NoneType .__ bases__'不會給'object'。 '__bases__'屬性是否與檢查子類相關? – overexchange

+1

@overxchange'type(None).__ bases__ ==(object,)'對我來說,在python2.7和python3.5 – o11c

1

是的類型,但我只能版本3.4測試

Python 3.4.3 (default, May 5 2015, 17:58:45) 
[GCC 4.9.2] on cygwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> None.__class__ 
<class 'NoneType'> 
>>> issubclass(None.__class__, object) 
True