How does Python class inherit objects?

PythonServer Side ProgrammingProgramming

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'>,)
raja
Published on 25-Jan-2018 12:25:16
Advertisements