What is the difference between old style and new style classes in Python?
In Python, classes can be divided into two types: old-style and new-style. The main difference between the two is that new-style classes inherit from a built-in base class, object
, whereas old-style classes do not. This means that new-style classes have access to certain features, such as the __class__
attribute and the super()
function, that are not available in old-style classes.
Old-style classes are defined in Python versions 2.x, where x < 3 and new-style classes are defined in Python 3.x
Here's an example of an old-style class:
class OldStyleClass:
pass
Here's an example of a new-style class:
class NewStyleClass(object):
pass
or
class NewStyleClass:
pass
The later one is the same as the former one, since class definition in python3 automatically inherit from object.