Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does NoneType class inherit object class?

In the below class hierarchy,

enter image description here enter image description here

Does class NoneType inherits class object?

Note : python 3.5

like image 864
overexchange Avatar asked May 16 '26 23:05

overexchange


2 Answers

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.

like image 104
o11c Avatar answered May 18 '26 11:05

o11c


Yes.

The isinstance function can tell you this.

>>> isinstance(None, object)
True
like image 36
Chris Martin Avatar answered May 18 '26 12:05

Chris Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!