3

In the below class hierarchy,

enter image description here enter image description here

Does class NoneType inherits class object?

Note : python 3.5

overexchange
  • 15,768
  • 30
  • 152
  • 347

3 Answers3

3

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.

o11c
  • 15,265
  • 4
  • 50
  • 75
1

Yes.

The isinstance function can tell you this.

>>> isinstance(None, object)
True
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • 2
    Technically, instance checking is not the same thing as subclass checking, which is also not the same thing as mro checking. – o11c Apr 05 '16 at 01:22
  • @o11c `str.__bases__` gives `object`. But `NoneType.__bases__` does not give `object`. Is `__bases__` property relevant to check subclass? – overexchange Apr 05 '16 at 01:24
  • 1
    @overexchange `type(None).__bases__ == (object,)` for me on python2.7 and python3.5 – o11c Apr 05 '16 at 01:30
1

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
Warren Spencer
  • 324
  • 2
  • 7