Comparing Old-Style and New-Style Classes in Python
Last Updated :
28 May, 2024
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.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read