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 Overflow
🌐
Daily Dose of DS
blog.dailydoseofds.com › p › dataclass-post-init
__Post_init__: Add Attributes To A Dataclass Object Post Initialization
December 19, 2022 - As the name suggests, this method is invoked right after the __𝐢𝐧𝐢𝐭__ method. This is useful if you need to perform additional setups on your dataclass instance. Share this post on LinkedIn: Link.
🌐
GitHub
github.com › ericvsmith › dataclasses › issues › 17
Should it be possible to pass parameter(s) to the post-init function? · Issue #17 · ericvsmith/dataclasses
June 10, 2017 - It would be nice to pass parameters to __init__ which do not initialize fields, but are accessible to the post-init function. But, it might mess up the interface too much, and there are issues with calling __dataclass_post_init__'s super() function if we decide to change the number of parameters.
Author   ericvsmith
Discussions

Bad dataclass post-init example
BPO 44365 Nosy @ericvsmith, @akulakov, @MicaelJarniac Files dataclass_inheritance_test.py: Example of Data Class Inheritancedataclass_inheritance_v2_test.py: Example of Data Class Inheritance (Upda... More on github.com
🌐 github.com
13
March 6, 2021
Python 3 dataclass initialization - Stack Overflow
Do dataclasses have a way to add ... some values of a list of integers that is one of the fields in the data class upon initialization. ... As described in the dataclass PEP, there is a __post_init__ method, which will be the last thing called by __init__.... More on stackoverflow.com
🌐 stackoverflow.com
Clean Code Writing: Dataclasses __post_init__ question
Do you actually need to make them separate instance variables? Why not keep them in self.data and access them from there? More on reddit.com
🌐 r/learnpython
12
4
July 16, 2023
Default __post_init__ Implementation in Dataclasses - Ideas - Discussions on Python.org
When using inheritance with dataclasses, if a parent class does not define a __post_init__ method, calling super().__post_init__() in the child class will raise an AttributeError, which is in contrast to super().__init__() Here’s a minimal example to illustrate the issue: from dataclasses ... More on discuss.python.org
🌐 discuss.python.org
4
July 4, 2024
🌐
Python
docs.python.org › 3 › library › dataclasses.html
dataclasses — Data Classes
3 weeks ago - If no __init__() method is generated, then __post_init__() will not automatically be called. Among other uses, this allows for initializing field values that depend on one or more other fields. For example: @dataclass class C: a: float b: float c: float = field(init=False) def __post_init__(self): self.c = self.a + self.b
🌐
GitHub
github.com › python › cpython › issues › 88531
Bad dataclass post-init example · Issue #88531 · python/cpython
March 6, 2021 - [3.12] gh-88531 Fix dataclass __post_init__/__init__ interplay documentation (gh-107404) #114162
Author   MicaelJarniac
🌐
TestDriven.io
testdriven.io › tips › 94ca0d2c-4c0d-4b8f-9762-6daa11d504f7
Tips and Tricks - Python - __post_init__ method in Dataclasses | TestDriven.io
from dataclasses import dataclass, field @dataclass class Order: net: float vat: float total: float = field(init=False) def __post_init__(self): self.total = self.net + self.vat order = Order(net=100.00, vat=22.00) print(order) # => Order(net=100.0, vat=22.0, total=122.0)
Find elsewhere
🌐
LogRocket
blog.logrocket.com › home › understanding python dataclasses
Understanding Python dataclasses - LogRocket Blog
June 4, 2024 - To enable comparison and sorting in a Python dataclass, you must pass the order property to @dataclass with the true value. This enables the default comparison functionality. Since we want to compare by population count, we must pass the population field to the sort_index property after initialization from inside the __post_innit__ method.
🌐
Medium
medium.com › @hadiyolworld007 › advanced-dataclass-post-init-hooks-that-save-hours-9fb76dfff915
Advanced dataclass: Post-Init Hooks That Save Hours | by Nikulsinh Rajput | Medium
September 8, 2025 - When you decorate a class with @dataclass, Python builds an __init__ for you. Immediately after that __init__ runs, it calls __post_init__(self).
🌐
Reddit
reddit.com › r/learnpython › clean code writing: dataclasses __post_init__ question
r/learnpython on Reddit: Clean Code Writing: Dataclasses __post_init__ question
July 16, 2023 -

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!

🌐
Python.org
discuss.python.org › ideas
Default __post_init__ Implementation in Dataclasses - Ideas - Discussions on Python.org
July 4, 2024 - I’ve encountered an issue with the dataclasses module that I believe could be improved. When using inheritance with dataclasses, if a parent class does not define a __post_init__ method, calling super().__post_init__() in the child class will raise an AttributeError, which is in contrast to super().__init__() Here’s a minimal example to illustrate the issue: from dataclasses import dataclass, field @dataclass class Parent: greetP: str = field(default="Hello from Parent") # No __post_...
🌐
Python Morsels
pythonmorsels.com › customizing-dataclass-initialization
Customizing dataclass initialization - Python Morsels
October 18, 2024 - There's a better way to perform initialization steps on a dataclass. dataclasses support a __post_init__ method, which is automatically called by the default dataclass initializer method:
🌐
Mastering-python
mastering-python.com › blog › 2023 › using-post-init-with-dataclasses
How to use post-init with dataclasses - Mastering Python
from dataclasses import dataclass, field @dataclass class Name: first: str last: str full: str = field(init=False) def __post_init__(self): self.full = f"{self.first} {self.last}" def main(): name = Name(first="John", last="Doe") print(name.full) if __name__ == "__main__": main()
🌐
GeeksforGeeks
geeksforgeeks.org › python › data-classes-in-python-set-5-post-init
Data Classes in Python | Set 5 (post-init) - GeeksforGeeks
July 11, 2025 - So author_name is dependent on profile handle which author attribute receives, so using __post_init__() should be an ideal choice this case. ... from dataclasses import dataclass, field name = {'vibhu4agarwal': 'Vibhu Agarwal'} @dataclass class GfgArticle: title : str language: str author: str author_name: str = field(init = False) upvotes: int = 0 def __post_init__(self): self.author_name = name[self.author] dClassObj = GfgArticle("DataClass", "Python3", "vibhu4agarwal") print(dClassObj) Output:
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
How to use Python dataclasses | InfoWorld
October 22, 2025 - Setting a field’s type to InitVar (with its subtype being the actual field type) signals to @dataclass to not make that field into a dataclass field, but to pass the data along to __post_init__ as an argument.
🌐
GitHub
github.com › python › cpython › issues › 111500
dataclass, slots, __post_init__ and super · Issue #111500 · python/cpython
October 30, 2023 - from dataclasses import dataclass @dataclass(slots=True) class Base: def __post_init__(self): pass @dataclass(slots=True) class Thing(Base): a: int b: int c: int = 0 def __post_init__(self): self.c = self.a + self.b super().__post_init__() t = Thing(1,3)
Author   voidspace
🌐
Hacker News
news.ycombinator.com › item
Data Classification: Does Python still have a need for class without dataclass? | Hacker News
February 15, 2023 - Whether you're using Kotlin data class, a C# record, a Scala case class, or whatever, we've seen in practice that these are only good for (often immutable) public state and data composition. Business objects with more logic and more than simple state/member manipulation are only hampered by ...
🌐
Earthly
earthly.dev › blog › more-on-python-data-classes
Let's Learn More About Python Data Classes - Earthly Blog
July 19, 2023 - Say you’d like to add an email field, a string of the form first_name.last_name@uni.edu (not super fancy, but works!). And we don’t want the users of the class to initialize this field, so we set init=False: from dataclasses import dataclass, field @dataclass class Student: first_name: str last_name: str major: str year: str gpa: float roll_num: str = field(default_factory=generate_roll_num, init=False) email: str = field(init=False)
🌐
Astral
docs.astral.sh › ruff › rules › post-init-default
post-init-default (RUF033) | Ruff
from dataclasses import InitVar, dataclass @dataclass class Foo: bar: InitVar[int] = 0 def __post_init__(self, bar: int = 1, baz: int = 2) -> None: print(bar, baz) foo = Foo() # Prints '0 2'.