A Technical Guide to Python DataClasses

Published on:
August 3, 2022

As the name suggests, these are classes that act as data containers. But what exactly are they? In this blog, MarsDevs presents you with the introduction and technicalities of Python DataClasses. You can now define the classes with less code and more out-of-the-box functionality. MarsDevs illustrates how.

What do we mean by an object?

When you look around, you can see various objects. Some of them are pit bulls, MTBs, etc., including yourself being an object.

If you notice carefully, you will figure out that all objects around you have a state, behavior, and identity.

Simplifying this more, you can think of the "state" as the physical properties. In the case of an object pitbull, you can say that its color, size, and hunger are its state. While under "behavior," all the actions and functions of the object come under it. 

Dataclasses
Dataclasses

If we take the same object pitbull, then barking, fetching, and wagging tail can be considered as its behavior, and at last comes identity, which means every object has a unique name.

Therefore an object can be defined as the bundle of variables and related methods(functions), or this can also be denoted as an instance of any particular class.

What do we mean by a class?

Talking about objects, now let's come to what a class is.

As you saw, there are many objects around you. But more broadly, we can say these objects can be classified under a specific group like pitbull come under the category of dogs, and MTB comes under the category of bicycles. So in layman's language, you got to know what a class is, but let's see it technically now. 

A class can be defined as a blueprint or prototype that defines the variables or the methods(functions) common to all the objects of a similar kind. It provides a means of wrapping data and functionality together.

A class can be an in-built class like a String class, Integer class, or a user-defined class.

Dataclass definition
Dataclass definition

What is DataClass?

If you plan to work with object-oriented programming like Python or Java, you should know about using classes. 

Dataclasses are similar to regular classes that are customized toward storing state rather than containing a lot of logic (i.e., methods). You make a class consisting of primary attributes every time you make a dataclass.

What the dataclasses module does is that it makes it easy for you to create a dataclass; otherwise, you have to write a lot of boilerplate to create one.

Now before understanding the current structure and uses of python dataclasses, let us go back in time to feel the present better.

Let us understand the past regarding classes in Python:

In the version Python 2.7, we observed the following flaws:

  • There was no specific or deep-rooted, standard method of printing class attributes. This led to users creating their solutions, which were often incomplete, frail, slow, or complicated.
  • Python developers are accustomed to using simple syntax like print (xyz_variable) to display any attribute's value, and it should be no different for classes. But an effective and clean solution for printing class attributes depended on the knowledge of specific python features like vars, dir string formatting, etc. 
  • Although things like string formatting and list comprehension are worth learning, as stated above, it makes things complex for programmers who are used to the easy syntax of Python.
Dataclasses in python
Dataclasses in python

What about the adulting phase of Python?

  • Late but still, it's okay.

'PEP' stands for Python Enhancement Proposal. A PEP is a document that describes new features proposed for Python and documents aspects of Python, like design and style, for the community. There are plenty of them. 

In 2017, PEP 557 conceptualized DataClasses, which "exist primarily to store values which are accessible by attribute lookup," and nearly a year later, Python 3.7.0 officially offered an implementation.

DataClasses based on type hinting and type annotations were introduced in Python 3.5 and 3.6, respectively. 

Type hinting is a formal solution to statically indicate the type of a value within our Python code in PEP 484. 

Type annotations are used to indicate the data types of variables and input/outputs of functions and methods in PEP 526.

When it comes to working with objects, people expect to be able to predict data types consistently. While Python isn't strictly typed, the combination of type hinting and annotations helps users and IDEs(Integrated Development Environments) know what data types to expect when working through a larger codebase.

The codebase is a collection of human-written source code files to execute, run, or implement an application or software solution.

Python dataclass
Python dataclass
  • Laziness Knocks for Automation.

Another part of maturing is being honest about what you care about and how to spend more time focused on that.

 It may be just me, but I feel like DataClasses allows us to define classes with less code and more functionality out of the box but are always insufficiently valued. Maybe someone enjoys dealing with tedious processes.

The exact words from PEP 557 regarding the DataClass are "nothing special about the class: the decorator adds generated methods to the class and returns the same class it was given."

Join the revolution with MarsDevs!

A small startup can't have an in-house tech team from the start. Hiring, firing, trials, and errors are a waste of time. And it would help if you thought about practical solutions to automate your needs and scale faster. But how would you do that? Would you miss out on innovation?

Of course not! Outsourcing your tech needs might be the perfect next step for you. And MarsDevs assure you the best! With a team of dedicated developers, experts take over your tech needs, from development to deployment and management. We chip in anytime you want. Also, the clock is ticking. So, why wait? Let us know about your project now! And in case you want to get in touch to discuss possibilities, you can book your slot here.


Similar Posts