I found the issue where was conversation about an explict call of parent's __post_init__
method using super()
, but if I try this:
from dataclasses import dataclass
@dataclass
class A:
def __post_init__(self):
print("A")
@dataclass
class B(A):
pass
b = B()
it will output:
A
So, parents method works without explict call.
__post_init__
method of dataclass doesnt called?The issue under discussion is in how __init__
and __post_init__
differ.
object
defines __init__
; it doesn't do anything, but that means it's always safe to call super().__init__
in your own definitions of __init__
. You have to take care what arguments you pass to it, but the attribute lookup itself will always succeed.
object
does not define __post_init__
, so calling super().__post_init__
requires you to know whether at least one class in your MRO will define it, or that you check for it first, or you'll get an AttributeError
.
So __post_init__
, if defined, will always be called, but it's not safe to assume for explicit super().__post_init__
calls that the method exists.