Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Dataclasses create init and call post_init

  • __init__ is implemented and that's how the attributes are initialized
  • __post_init__ is called after __init__ to allow for further initializations
from dataclasses import dataclass

@dataclass
class Point():
    x : float
    y : float
    name : str

    def __post_init__(self):
        print(f"In post init: {self.name}")
from shapes import Point

p1 = Point(2, 3, 'left')