Make your Python classes less long-winded and more powerful with the use of Python dataclasses.

By Serdar Yegulalp | InfoWorld

Everything in Python is an object, or so the saying goes. If you want to create your own custom objects, with their own properties and methods, you use Python’s class object to make that happen. But creating classes in Python sometimes means writing loads of repetitive, boilerplate code to set up the class instance from the parameters passed to it or to create common functions like comparison operators.

Dataclasses, introduced in Python 3.7 (and backported to Python 3.6), provide a handy way to make classes less verbose. Many of the common things you do in a class, like instantiating properties from the arguments passed to the class, can be reduced to a few basic instructions.

Python dataclass example

Here is a simple example of a conventional class in Python:

class Book:
”’Object for tracking physical books in a collection.”’
def __init__(self, name: str, weight: float, shelf_id:int = 0):
self.name = name
self.weight = weight # in grams, for calculating shipping
self.shelf_id = shelf_id
def __repr__(self):
return(f”Book(name={self.name!r},
weight={self.weight!r}, shelf_id={self.shelf_id!r})”)

The biggest headache here is the way each of the arguments passed to __init__ has to be copied to the object’s properties. This isn’t so bad if you’re only dealing with Book, but what if you have to deal with Bookshelf, Library, Warehouse, and so on? Plus, the more code you have to type by hand, the greater the chances you’ll make a mistake.

Here is the same Python class, implemented as a Python dataclass:

from dataclasses import dataclass

@dataclass

class Book:

”’Object for tracking physical books in a collection.”’

name: str

weight: float

shelf_id: int = 0

When you specify properties, called fields, in a dataclass, @dataclass automatically generates all of the code needed to initialize them. It also preserves the type information for each property, so if you use a code linter like mypy, it will ensure that you’re supplying the right kinds of variables to the class constructor.

Another thing @dataclass does behind the scenes is automatically create code for a number of common dunder methods in the class. In the conventional class above, we had to create our own __repr__. In the dataclass, this is unnecessary; @dataclass generates the __repr__ for you.

Customize Python dataclass fields with the field function

The default way dataclasses work should be okay for the majority of use cases. Sometimes, though, you need to fine-tune how the fields in your dataclass are initialized. To do this, you can use the field function.

from dataclasses import dataclass, fieldfrom typing import List @dataclassclass Book:    ”’Object for tracking physical books in a collection.”’    name: str         condition: str = field(compare=False)        weight: float = field(default=0.0, repr=False)    shelf_id: int = 0    chapters: List[str] = field(default_factory=list)

When you set a default value to an instance of field, it changes how the field is set up depending on what parameters you give field.

 

To read the full article, click here. Learn how to control Python dataclass initialization, when to use Python dataclass and more.