Difference between DataClass vs NamedTuple vs Object in Python


Dataclass, NamedTuple, and Object are used to create structured datatypes in Python. Though all three are used to create structured data they differ in their properties and implementation method. In this article, we will understand the difference between DataClass, NamedTuple, and Object in Python.

Feature

Object

NamedTuple

Dataclass

Creation

Objects are created by defining a class and its attributes and methods manually.

Named tuples are created using the named tuple function from the collections module. The field names and values are specified manually.

Data classes are created using the @dataclass decorator. The class attributes and default values are specified as class variables.

Readability

Objects can be verbose and require a lot of boilerplate code.

Named tuples are created using the named tuple function from the collections module. The field names and values are specified manually.

Data classes are created using the @dataclass decorator. The class attributes and default values are specified as class variables.

Immutability

Objects are mutable by default but can be made immutable by using special methods or third-party libraries.

Named tuples are immutable by default, which can be useful for enforcing data integrity.

Data classes are mutable by default but can be made immutable by adding the frozen=True argument to the @dataclass decorator.

Automatic methods

Objects do not provide any automatic methods, such as __init__, __repr__, or __eq__. These must be defined manually.

Named tuples are immutable by default, which can be useful for enforcing data integrity.

Data classes are mutable by default but can be made immutable by adding the frozen=True argument to the @dataclass decorator

Inheritance

Objects can be used as base classes for other objects, allowing for code reuse and customization.

Named tuples do not support inheritance, but can be subclassed using the _replace method.

Data classes support inheritance and can be used as base classes for other data classes.

Default values

Objects do not provide default values for attributes.

Named tuples do not provide default values for attributes, but the field names and values can be specified using keyword arguments.

Data classes provide default values for attributes, which can be useful for handling missing or optional data.

Type hints

Objects do not provide type hints, but they can be added manually using comments or third-party libraries.

Named tuples do not provide type hints, but they can be added manually using comments or third-party libraries.

Data classes provide type hints automatically, based on the type annotations of the class attributes.

Object

The concept of object is used in object-oriented programming to represent any real-world entities in code. In Python, objects are created using classes. A class is the blueprint of an object which defines the attributes and methods of the object. An instance of a class is called an object.

Example

In the below example, we have created a Person class that contains the name and age attribute of the person. Then you can create an instance of the class like the objects p1 and p2 created below.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p1 = Person("John", 30)
p2 = Person("Mary", 25)

print(p1.name,p1.age,p2.name,p2.age)

Output

John 30 Mary 25

NamedTuple

NamedTuple is a built-in class in Python that is defined in the collection module of Python.It is created using named tuple factory function. The named tuple factory function takes two arguments- the tuple name and the sequence of field names.

Example

In the below example, we created a named tuple with the name Person and two fields - name and age. We create two instances of the named tuple and pass the value for name and age in both.

from collections import namedtuple

Person = namedtuple("Person", ["name", "age"])
p1 = Person("John", 30)
p2 = Person("Mary", 25)

print(p1.name)  
print(p2.age) 

Output

John
25

DataClass

Dataclass is a decorator in Python that simplifies the creation of classes that represents structured data.It is defined in the dataclass module of Python and is created using @dataclass decorator. It allows automatic generation of __init__,__repr__,eq functions.

Example

In the below example, a data class-named person is defined with three attributes - name, age, and city. Type hints are used to specify the datatypes of the attributes and also default value is set for the city attribute. We then created an instance for the person class and pass the value of the attribute as arguments.

from dataclasses import dataclass

@dataclass
class Person:
   name: str
   age: int
   city: str = "New York"

p1 = Person("John", 30)
p2 = Person("Mary", 25, "Los Angeles")

print(p1)  
print(p2) 

print(p1 == p2) 

Output

Person(name='John', age=30, city='New York')
Person(name='Mary', age=25, city='Los Angeles')
False

Conclusion

In this article, we discussed the differences between Dataclass, NamedTuple, and Objects in Python. Objects are used in object-oriented programming and Named tuples are the built-in class in the collection module of Python whereas Dataclass is a decorator that simplifies the creation of classes that represents structured data.

Updated on: 06-Jul-2023

660 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements