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.
Answer from ICW on Stack OverflowBad dataclass post-init example
Python 3 dataclass initialization - Stack Overflow
Clean Code Writing: Dataclasses __post_init__ question
Default __post_init__ Implementation in Dataclasses - Ideas - Discussions on Python.org
Videos
Hello,
I have a question about the best way to initialize my instance variables for a data class in python. Some of the instance variables depend on some of the fields of the data class in python, which are inputs to a webscraping method. This means I need a __post_init__ method to retrieve the values from the webscrape. For the __post_init__ method, I would have way more than 3 variables being scraped from the website, so getting the key variable from data seems really inefficient. I know there are fields you can add to dataclasses, but I am not sure if that would help me here. Is there anyway I can simplify this? Here is my code (This is not the actual code, just the general structure of the dataclass):
from dataclasses import dataclass
from external_scrape_module import run
@dataclass
class Scrape:
path: int
criteria1: str
criteria2: str
criteria3: str
def __post_init__(self) -> None:
self.data: dict = self.scrape_website()
self.scraped_info1: str = self.data['scraped_info1']
self.scraped_info2: str = self.data['scraped_info2']
self.scraped_info3: str = self.data['scraped_info3']
def scrape_website(self) -> dict:
return run(self.path, self.criteria1, self.criteria2, self.criteria3)Much help would be appreciated, as I am fairly new to dataclasses. Thanks!