Open In App

Comparing Old-Style and New-Style Classes in Python

Last Updated : 28 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, the difference between old-style and new-style classes is based on the inheritance from the built-in object class. This distinction was introduced in Python 2.x and was fully adopted in Python 3.x, where all classes are new-style classes. In this article, we will see the difference between the old-style and new-style classes in Python.

Python Old-Style Classes

In Python 2.x, old-style classes are those that do not explicitly inherit from the object class and thus have some limitations in terms of functionality and performance.

Syntax:

class OldStyleClass:
pass

Following are the characteristics of the Python old-style classes:

  • Do not support some advanced features such as descriptors, __slots__, and the new method resolution order (MRO).
  • The MRO is implemented differently, using a depth-first search algorithm.

Python New-Style Classes

The New-style classes are those that explicitly inherit from the object class or any other built-in type that ultimately inherits from the object. It unify the concepts of types and classes, offering more features and a more consistent object-oriented model.

Syntax:

class NewStyleClass(object):
pass

In Python 3.x, the object inheritance is implicit, so the following is also a new-style class:

class NewStyleClass:
pass

The following are the characteristics of the Python new-style classes:

  • Support advanced features such as descriptors, __slots__, properties, and the new method resolution order (C3 linearization).
  • Provide a unified type hierarchy where all types derive from a common base class (object).
  • More consistent and predictable behavior in multiple inheritance scenarios.

Differences between Old-Style Classes and New-Style Classes

Feature

Old-Style Classes

New-Style Classes

Definition

Do not inherit from an object.

Inherit from an object or another new-style class.

Syntax

class OldStyleClass:

class NewStyleClass(object):

class NewStyleClass:

Type of Instance

<type 'instance'>

The class itself, e.g., <class '__main__.NewStyle'>

Inheritance

No explicit inheritance from the object.

Explicit inheritance from an object or implicitly in Python 3.

Method Resolution Order

Depth-first, left-to-right.

C3 linearization (more predictable and consistent).

Descriptors and Properties

Limited or no support.

Full support for descriptors, properties, and __slots__.

Built-in Functions

Some built-in functions behave differently.

Full support for special methods, including __getattr__, __setattr__, etc.

Super() Function

super() does not work.

Used to call methods from a parent class.

Class Attribute

Instances have __class__ attribute but less useful.

Instances have __class__ attribute that points to the class.

Use in Python 3

Not applicable (Python 3 does not have old-style classes).

All classes are new-style by default.

Working on Old-Style and New-Style Classes in Python

Understanding the differences between old-style and new-style classes in Python is essential for working with different versions of Python and maintaining legacy code. Now let us see an example of both, old-style class and new-style on different Python versions for a better understanding.

Python 2.x

In Python 2.x, the type() function returns the type of the object. For old-style class instances, it returns <type 'instance'>. This is because old-style classes do not have the same rich type information as new-style classes. Whereas for new-style class, it returns the actual class of the instance in this case, <class '__main__.NewStyle'>. The __main__ indicates that this class is defined in the main module.

Python
# Old-style class
class OldStyle:
    pass

# New-style class
class NewStyle(object):
    pass

# Instances
old_instance = OldStyle()
new_instance = NewStyle()

print(type(old_instance))  
print(type(new_instance)) 
print(isinstance(new_instance, object))

Output:

<type 'instance'>
<class '__main__.NewStyle'>
True

Python 3.x

In Python 3.x, all classes are new-style classes, and the distinction between old-style and new-style classes no longer exists. Thus, all classes in Python 3 automatically inherit from object, providing the benefits of new-style classes by default.

Python
# Old-style class
class OldStyle:
    pass

# New-style class
class NewStyle(object):
    pass

# Instances
old_instance = OldStyle()
new_instance = NewStyle()

print(type(old_instance))  
print(type(new_instance)) 
print(isinstance(new_instance, object))

Output:

<class '__main__.OldStyle'>
<class '__main__.NewStyle'>
True

Conclusion

Old-style classes are a legacy concept from Python 2 that lacks many modern features and consistent behavior, while new-style classes, introduced in Python 2.x and used exclusively in Python 3.x, offer a unified and feature-rich class model. If you are working with Python 3, you do not need to worry about this distinction, as all classes are new-style by default.


Next Article
Article Tags :
Practice Tags :

Similar Reads