Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 dataclass initialization

Do dataclasses have a way to add additional initialization aside from what is provided with its built in initializer, without overriding it? Specifically, I want to check some values of a list of integers that is one of the fields in the data class upon initialization.

like image 220
ICW Avatar asked Jul 05 '18 20:07

ICW


People also ask

How do you initialize a Dataclass in Python?

A Python class is a program used to create objects and their properties. We use the keyword class to create a class in Python. For class attributes to be initialized, we use a constructor method called __init__() which is called when an object is created in a Python class.

What is __ Post_init __ Python?

Modifying fields after initialization with __post_init__ The __post_init__ method is called just after initialization. In other words, it is called after the object receives values for its fields, such as name , continent , population , and official_lang .

Can Python Dataclasses have methods?

A dataclass can very well have regular instance and class methods. Dataclasses were introduced from Python version 3.7. For Python versions below 3.7, it has to be installed as a library.

How do you initialize fields in a Python class?

Here is the same Python class, implemented as a Python dataclass: When you specify properties, called fields, in a dataclass, @dataclass automatically generates all of the code needed to initialize them.

How to use Python dataclasses?

How to use Python dataclasses 1 Python dataclass example. ... 2 Customize Python dataclass fields with the field function. ... 3 Use __post_init__ to control Python dataclass initialization. ... 4 Use InitVar to control Python dataclass initialization. ... 5 When to use Python dataclasses — and when not to use them. ...

How do you initialize the fields in your dataclass?

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. 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.

What is a dataclasscard in Python?

A data class is a class typically containing mainly data, although there aren’t really any restrictions. It is created using the new @dataclass decorator, as follows: from dataclasses import dataclass @dataclass class DataClassCard: rank: str suit: str


1 Answers

As described in the dataclass PEP, there is a __post_init__ method, which will be the last thing called by __init__.

from dataclasses import dataclass

@dataclass
class DataClass: 
    some_field: int

    def __post_init__(self):
        print(f"My field is {self.some_field}")

Defining this dataclass class, and then running the following:

dc = DataClass(1) # Prints "My field is 1"

Would initialize some_field to 1, and then run __post_init__, printing My field is 1.

This allows you to run code after the initialization method to do any additional setup/checks you might want to perform.

like image 125
ICW Avatar answered Oct 09 '22 03:10

ICW