Wednesday, November 17, 2021

Python for Beginners: Dataclass in Python

While programming in python, you might have used classes to create different objects. Classes in python are very helpful in depicting real world objects in our programs. In this article, we will discuss a decorator named Dataclass with which we can modify the properties of a class. We will also discuss how a dataclass is important  while programming in python.

What is a dataclass?

Dataclass is a decorator defined in the dataclasses module. It was introduced in python 3.7. A dataclass decorator can be used to implement classes that define objects with only data and very minimal functionalities. 

A class defined using dataclass decorator has very specific uses and properties that we will discuss in the following sections. Let us first discuss how we can implement a class using dataclass in python.

How to use dataclass in Python?

The dataclass decorator has been defined in the dataclasses module. You can first install the dataclasses module using PIP as follows.

pip3 install --upgrade dataclasses

After installing the dataclasses module, you can import the dataclass decorator using the import statement as follows.

from dataclasses import dataclass

Let us now define a class with dataclass decorator. 

from dataclasses import dataclass


@dataclass
class Person:
    Name: str
    Country: str
    Age: int


candidate = Person("Joe Biden", "USA", 78)
print("The candidate is:",candidate)

Output:

The candidate is: Person(Name='Joe Biden', Country='USA', Age=78)

You may notice that we have specified the data type of class attributes in the above code. Moreover, we do not need to implement the __init__() constructor while using the dataclass decorator. The decorator itself implements the __init__() method for us. 

Benefits of using dataclass in Python

We can define classes using dataclass decorator to represent objects.

If we want to print the attributes of the object using the print statement, we have to use the __repr__() method when we have implemented a class without using the dataclass decorator. Otherwise, the output is as follows.

class Person:
    def __init__(self, name, country, age):
        self.Name = name
        self.Country = country
        self.Age = age


candidate = Person("Joe Biden", "USA", 78)
print("The candidate is:", candidate)

Output:

The candidate is: <__main__.Person object at 0x7fb7289a8070>

To print the class attributes, we will have to implement the __repr__() method as follows.

class Person:
    def __init__(self, name, country, age):
        self.Name = name
        self.Country = country
        self.Age = age

    def __repr__(self):
        return "Name: {}, Country: {}, Age: {}".format(self.Name, self.Country, self.Age)


candidate = Person("Joe Biden", "USA", 78)
print("The candidate is:", candidate)

Output:

The candidate is: Name: Joe Biden, Country: USA, Age: 78

But, when we use the dataclass decorator, all the class attributes are printed without implementing the __repr__() method. This can be observed in the following example.

from dataclasses import dataclass


@dataclass
class Person:
    Name: str
    Country: str
    Age: int


candidate = Person("Joe Biden", "USA", 78)
print("The candidate is:",candidate)

Output:

The candidate is: Person(Name='Joe Biden', Country='USA', Age=78)

Another major difference between a simple class and a class with dataclass decorator is the way in which the instances of the class are compared. 

For example, When we create a class and compare its instances using the == operator, The python interpreter checks the identity or memory location of the objects and they are considered equal only if both the instances refer to the same memory location. This can be observed in the following program.

class Person:
    def __init__(self, name, country, age):
        self.Name = name
        self.Country = country
        self.Age = age

    def __repr__(self):
        return "Name: {}, Country: {}, Age: {}".format(self.Name, self.Country, self.Age)


candidate1 = Person("Joe Biden", "USA", 78)
candidate2 = Person("Joe Biden", "USA", 78)

print("Candidate 1 is:", candidate1)
print("Candidate 2 is:", candidate2)

print("Both the candidates are same?", candidate1 == candidate2)

Output:

Candidate 1 is: Name: Joe Biden, Country: USA, Age: 78
Candidate 2 is: Name: Joe Biden, Country: USA, Age: 78
Both the candidates are same? False

Here, you can see that Candidate 1 and Candidate 2 are considered different because they are different objects and refer to different memory locations.

On the contrary, when we define a class using the dataclass decorator, the comparison operator works very differently. When we compare two instances of the class using the == operator, the values in the class attributes of the objects are compared instead of the memory location. If the values in the corresponding attributes are equal in both instances, the objects are said to be equal. You can observe this in the following program.

from dataclasses import dataclass


@dataclass
class Person:
    Name: str
    Country: str
    Age: int


candidate1 = Person("Joe Biden", "USA", 78)
candidate2 = Person("Joe Biden", "USA", 78)
print("The candidate 1 is:", candidate1)
print("The candidate 2 is:", candidate2)
print("Both the candidates are same?", candidate1 == candidate2)

Output:

The candidate 1 is: Person(Name='Joe Biden', Country='USA', Age=78)
The candidate 2 is: Person(Name='Joe Biden', Country='USA', Age=78)
Both the candidates are same? True

Here, both the candidates are considered equal because the attributes of the objects are equal. Thus we can easily compare data inside objects when we implement classes using the dataclass decorator.

You can see that the dataclass decorator gives us a better method to compare objects. Otherwise, we will have to define methods to compare the objects. This may result in costly execution in terms of time and space.

Conclusion

In this article, we have discussed the dataclass decorator in python. We have also implemented it and saw some of its peculiar properties that make it a useful construct to use in our programs. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.

The post Dataclass in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...