DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Black Box Tester in Python
  • Modern Python: Patterns, Features, and Strategies for Writing Efficient Code (Part 1)
  • API Appliance for Extreme Agility and Simplicity
  • Python for Beginners: An Introductory Guide to Getting Started

Trending

  • Using My New Raspberry Pi To Run an Existing GitHub Action
  • Continuous Improvement as a Team
  • Building a Performant Application Using Netty Framework in Java
  • Architecture: Software Cost Estimation
  1. DZone
  2. Coding
  3. Languages
  4. Understanding Python's dataclass Decorator

Understanding Python's dataclass Decorator

This tutorial explores the advantages, usage, ordering, immutability, and default value of Python's dataclass decorator.

By 
Sameer Shukla user avatar
Sameer Shukla
DZone Core CORE ·
Jan. 29, 24 · Tutorial
Like (3)
Save
Tweet
Share
2.6K Views

Join the DZone community and get the full member experience.

Join For Free

@dataclass is a decorator which is part of the Python dataclasses module. When the @dataclass decorator is used, it automatically generates special methods such as: 

  • _ _ init _ _.: Constructor to initialize fields
  • _ _ repr _ _ : String representation of the object
  • _ _ eq _ _  : Equality comparison between objects
  • _ _ hash_ _ : Enables use as dictionary keys (if values are hashable)

 Along with the methods listed above, the @dataclass decorator has two important attributes.

  • Order: If True, (the default is False), __lt__(), __le__(), __gt__(), and __ge__() methods will be generated; i.e., @dataclass (order = True).
  • Immutability: Fields can be made immutable using the frozen=True parameter; i.e., @dataclass(frozen=True). 

 In a nutshell, the primary goal of the @dataclass decorator is to simplify the creation of classes. 

Advantages of the dataclass Decorator

Using the dataclass decorator has several advantages:

  • Boilerplate reduction: It reduces the amount of boilerplate code needed for classes by automatically generating common special methods.
  • Readability: It improves the readability of the code by making it more concise and focused on the data representation.
  • Default values: You can provide default values for attributes directly in the class definition, reducing the need for explicit __init__() methods.
  • Immutability: By combining @dataclass with the frozen=True option, you can create immutable data classes, ensuring that instances cannot be modified after creation.

Usage

Python
 
from dataclasses import dataclass

@dataclass
class Person:
    name: str
    age: int


In this example, the Person class is annotated with @dataclass, and two fields (name and age), are declared. The __init__(), __repr__(), __eq__(), and __hash__() methods are automatically generated. Here's an explanation of how to use each generated method:

  • __init__(self, ...): The __init__ method is automatically generated with parameters corresponding to the annotated attributes. You can create instances of the class by providing values for the attributes.
Python
 
person = Person('Sam', 45)


  • __repr__(self) -> str: The __repr__ method returns a string representation of the object, useful for debugging and logging. When you print an object or use it in an f-string, the __repr__ method is called.
Python
 
person # Person(name='Sam', age=45)


  • __eq__(self, other) -> bool: The __eq__ method checks for equality between two objects based on their attributes. It is used when you compare objects using the equality operator (==).
Python
 
# Usage
person1 = Person('Sam', 45)
person1

person2 = Person('Sam', 46)
person2

print(person1 == person2) # False. 


  • __hash__(self) -> int: The __hash__ method generates a hash value for the object, allowing instances to be used in sets and dictionaries. It is required when the class is used as a key in a dictionary or an element in a set.

Ordering

If you include the order=True option, additional ordering methods (__lt__, __le__, __gt__, and __ge__) are generated. These methods allow instances to be compared using less than, less than or equal, greater than, and greater than or equal operators. If you perform a comparison on the Person object without order, TypeError will be thrown.  

Python
 
print(person1 < person2)
# // TypeError: '<' not supported between instances of 'Person' and 'Person'


After adding ordering, we can perform comparisons.

Python
 
@dataclass(order=True)
class Person:
    name: str
    age: int


# Usage
person1 = Person('Sam', 45)
person1

person2 = Person('Sam', 46)
person2

print(person1 < person2) # False. 


order is False by default, meaning comparison methods are not generated unless explicitly enabled. Comparisons are based on field values, not object identities.

Immutability

@dataclass can be made immutable using the frozen=True attribute; the default is False.

Python
 
@dataclass
class Person:
    name: str
    age: int

person = Person('Sam', 45)
person.name = 'Sam2'
person # Person(name='Sam2', age=45)


In the code above, we are able to reassign values to the Person name field. After adding frozen=True, the exception will be thrown and reassignment is not allowed. 

Python
 
@dataclass(frozen=True)
class Person:
    name: str
    age: int

person = Person('Sam', 45)
person.name = 'Sam2'

# FrozenInstanceError: cannot assign to field 'name'


Be aware of performance implications: frozen=True adds a slight overhead because of additional checks for immutability.

Default Value

Using the dataclasses module, we can assign the default value to the fields in the class definition. 

Python
 
from dataclasses import dataclass, field

@dataclass
class Person:
    name: str
    age: int = field(default=20)


# Usage
person = Person('Sam')
person # Person(name='Sam', age=20)


Default values can be of any data type, including other data classes or mutable objects. They are evaluated only once when the class is defined, not each time an instance is created.

Python (language) Function type

Opinions expressed by DZone contributors are their own.

Related

  • Black Box Tester in Python
  • Modern Python: Patterns, Features, and Strategies for Writing Efficient Code (Part 1)
  • API Appliance for Extreme Agility and Simplicity
  • Python for Beginners: An Introductory Guide to Getting Started

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: