How does Python class inherit objects?


In Python 2.x there are two styles of classes depending on the presence or absence of a built-in type as a base-class −

‘Old style’ or "Classic" style classes: they have no built-in type as a base class −

>>> class OldFoo:      # no base class
...     pass
>>> OldFoo.__bases__
()

"New" style classes: they have a built-in type as a base class meaning that, directly or indirectly, they have object as a base class −

>>> class NewFoo(object):           # directly inherit from object
...    pass
>>> NewFoo.__bases__
(<type 'object'>,)

In Python 3.x however, only new style classes allowed that have built-in type as a base-class − 

There is no need to mention object in parentheses after the class name here in class declaration. Here classes inherit from object implicitly.

 >>> class Foo:           # directly inherit from object
...    pass
>>> Foo.__bases__
(<type 'object'>,)

Updated on: 16-Jun-2020

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements