0
0
PythonComparisonBeginner · 4 min read

Dataclass vs namedtuple in Python: Key Differences and Usage

In Python, dataclass provides a flexible and mutable way to define classes with automatic method generation, while namedtuple creates immutable, lightweight objects mainly for simple data storage. dataclass supports default values and methods, making it more versatile than namedtuple.
⚖️

Quick Comparison

This table summarizes the main differences between dataclass and namedtuple in Python.

Featuredataclassnamedtuple
MutabilityMutable by defaultImmutable
SyntaxClass-based with decoratorsFunction call returning tuple subclass
Default ValuesSupports default valuesSupports default values (Python 3.7+) but less flexible
MethodsCan define custom methodsLimited to tuple methods
PerformanceSlightly slower due to class overheadFaster and lightweight
Use CaseComplex data with behaviorSimple, fixed data containers
⚖️

Key Differences

dataclass is a modern Python feature introduced in Python 3.7 that uses a decorator to automatically add special methods like __init__, __repr__, and __eq__ to user-defined classes. It allows mutable fields by default, meaning you can change the values of attributes after creation. You can also add default values and define your own methods inside the class, making it very flexible for complex data structures.

On the other hand, namedtuple is a factory function from the collections module that creates tuple subclasses with named fields. These objects are immutable, so once created, their values cannot be changed. While you can provide default values starting from Python 3.7, it is less straightforward than with dataclass. Namedtuples are lightweight and fast, ideal for simple, fixed data containers without behavior.

In summary, use dataclass when you need mutable objects with methods and defaults, and namedtuple when you want simple, immutable records with minimal overhead.

⚖️

Code Comparison

Here is how you define a simple data structure for a point with dataclass:

python
from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

p = Point(10, 20)
print(p)
p.x = 15
print(p)
Output
Point(x=10, y=20) Point(x=15, y=20)
↔️

namedtuple Equivalent

Here is the equivalent using namedtuple:

python
from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p)

# The following line would raise an error because namedtuples are immutable
# p.x = 15
Output
Point(x=10, y=20)
🎯

When to Use Which

Choose dataclass when you need mutable objects, want to add methods, or require default values and more complex behavior. It is best for data models that might change or need additional functionality.

Choose namedtuple when you want a simple, immutable, and lightweight container for fixed data without extra methods. It is ideal for performance-sensitive code or when immutability is important.

Key Takeaways

dataclass offers mutable, flexible classes with automatic method generation.
namedtuple creates immutable, lightweight objects ideal for simple data storage.
Use dataclass for complex data with behavior; use namedtuple for fixed, simple records.
dataclass supports default values and custom methods; namedtuple has limited customization.
namedtuple is generally faster and uses less memory than dataclass.