Skip to content

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:

An old-style class in Python

python
class OldStyleClass:
    pass

Here's an example of a new-style class:

A new-style class in Python

python
class NewStyleClass(object):
    pass

or

New versions also support the old-style classes in Python

python
class NewStyleClass:
    pass

The later one is the same as the former one, since class definition in python3 automatically inherit from object.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.