Understand python's dataclasses (1) - iditect.com

July 08, 2022 | Admin |

If you are reading this, then you are aware of python 3.7 and the new features it contains. Personally, I'm very excited about dataclasses because I've been waiting for it for a while.

This series consists of two parts:

 

dataclasses are python classes (lctt Annotation: more precisely, it is a module), suitable for storing data objects. You might ask what is a data object? The following is a less detailed list of properties that define a data object:

Of course there are many more features, but this list is enough to help you understand the crux of the problem.

To understand dataclasses , we will implement a simple class that holds numbers and allows us to perform the operations mentioned above.

First, we'll use normal classes, and then we'll use dataclasses to achieve the same result.

But before we start, let's talk about the usage of dataclasses .

Python 3.7 provides a decorator dataclass for converting classes to dataclass .

All you have to do is wrap the class in a decorator:

Now, let's dive into the changes and uses that dataclass brings us.

 

Usually this is:

With dataclass like this:

Here are the changes brought by the dataclass decorator:

It is also possible to define default values:

 

Object representation refers to a meaningful string representation of an object, which is useful when debugging.

The default python object representation is not very intuitive:

This leaves us with no idea what the object is doing, and can lead to a bad debugging experience.

A meaningful representation can be achieved by defining a __repr__ method in the class.

Now we get a meaningful representation of this object:

dataclass  automatically adds a __repr__  function so that we don't have to implement it manually.

 

Often, data objects need to be compared with each other.

A comparison between two objects a and b usually involves the following operations:

In python, it is possible to define methods in classes that can do the above. For the sake of simplicity and not to make this article too verbose, I will only show the implementation of == and < .

Usually written like this:

Use  dataclass :

Yes, it's that simple.

We don't need to define the __eq__ and __lt__  methods because the dataclass decorator automatically adds them to our class definition when order = true is called .

So how does it do it?

When you use dataclass , it adds functions __eq__  and  __lt__ to the class definition  . We already know this. So how do these functions know how to check for equality and compare?

The dataclass class that generates the __eq__ function compares a tuple of two attributes, one consisting of its own attributes and the other consisting of attributes of other instances of the same class. In our case, the auto- generated  __eq__ function is equivalent to:

Let's see a more detailed example:

We will write a dataclass class person to hold the name and age .

The auto-generated __eq__  method is equivalent to:

Note the order of properties. They are always generated in the order you define them in the dataclass class.

Again, the equivalent __le__ function looks like:

Definitions of functions like __le__ usually appear when you need to sort a list of data objects . Python's built-in sorted function relies on comparing two objects.

It's not always worthwhile to define all the dunder methods. Your use case might only consist of storing values ​​and checking for equality. Therefore, you only need to define __init__ and __eq__ methods. If we could tell the decorator not to generate other methods, it would reduce some overhead and we would have the correct operations on the data object.

Fortunately, this can be achieved by making the dataclass  decorator a callable.

We'll discuss frozen next . It deserves a separate article due to the complex use case of the unsafe_hash parameter.

Now back to our use case, here's what we need:

1.  __init__ 2.  __eq__

These functions are generated by default, so what we need is to not generate other functions. So what should we do? It's as simple as passing the relevant parameter as false to the generator.

 

A frozen instance is an object whose properties cannot be modified after the object has been initialized.

Creating immutable properties of objects in python is a daunting task that I won't delve into in this post.

Here's what we expect immutable objects to be able to do:

With dataclass , a frozen object can be defined by using the dataclass decorator as a callable with the parameter frozen=true .

When instantiating a frozen object, any attempt to modify the object's properties will raise frozeninstanceerror .

Therefore, a frozen instance is a good way to store:

These generally do not change during the lifetime of the application and any attempt to modify them should be prohibited.

 

With dataclass , the need to define an __init__ method to assign the variable to self has already been handled. But we lose the flexibility of function calls or processing that is required immediately after the variable is assigned a value.

Let's discuss a use case in which we define a float class to hold floating point numbers and then compute the integer and fractional parts right after initialization.

Usually this is:

Fortunately, post_init operations are already handled using the post_init   method.

The generated __init__   method returns by calling __post_init__ before returning. So any processing can be done in the function.

How convenient!

 

dataclasses support inheritance, just like normal python classes.

Therefore, properties defined in the parent class will be available in the child class.

Note that the argument to student  is the order of the fields defined in the class.

How does __post_init__ behave during inheritance ?

Since __post_init__ is just another function, it must be called the traditional way:

In the above example, only b 's __post_init__ is called, so how do we call a 's __post_init__ ?

Because it is a function of the parent class, it can be called with super .

 

So, above are a few ways that dataclasses can make things easier for python developers.

Professional provider of PDF & Microsoft Word and Excel document editing and modifying solutions, available for ASP.NET AJAX, Silverlight, Windows Forms as well as WPF. We are dedicated to provide powerful & profession PDF/Word/Excel controls.

Related Posts

Vocational courses in Canada | Education in Canada

Popular vocational courses in Canada for international students: Nursing, Massage therapy, Medical office assisting, Fitness & nutrition, Emergency medical technician, Pharmacy technician training, Health Science assistance. Theory-based practical learning. After completion, students must register as an apprentice with the Canadian Department ...

support@studyincanada.college

By pursuing vocational courses in Canada, international students are choosing a career-oriented experience that is perfectly in sync with today’s dynamic job market. Studying in Canada is not just about getting a degree but it is about the...

Read more >>