In the below class hierarchy,
Does class NoneType
inherits class object
?
Note : python 3.5
Yes, in both Python2 and Python3:
>>> type(None)
<class 'NoneType'>
>>> type(None).mro()
[<class 'NoneType'>, <class 'object'>]
>>> issubclass(type(None), object)
True
>>> isinstance(None, object)
True
Note that in Python2, the only classes that are not subclasses of object
are old-style classes. However, instances of such classes are still instances of 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
Edit: I suspect some things might be different for Python < 2.6 and possibly for types implemented in C.
Yes, though I can only test with version 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