Author MicaelJarniac
Recipients MicaelJarniac, docs@python, eric.smith
Date 2021-06-11.02:17:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1623377860.89.0.382005265925.issue44365@roundup.psfhosted.org>
In-reply-to
Content
Well, at least for this example, to call `super().__init__()`, I'd need to provide it the two arguments it expects, `x` and `y`, otherwise it'd give an error:

> TypeError: __init__() missing 2 required positional arguments: 'x' and 'y'

If I try calling it as `super().__init__(self.x, self.y)`, I get an infinite recursion error:

> RecursionError: maximum recursion depth exceeded while calling a Python object

That's mostly why I've chosen to call `__post_init__` instead.

And if we're dealing with `InitVar`s, they can nicely be chained like so:

>>> from dataclasses import dataclass, field, InitVar
>>>
>>> @dataclass
... class A:
...     x: int
...     y: InitVar[int]
...     xy: int = field(init=False)
...
...     def __post_init__(self, y: int) -> None:
...         self.xy = self.x * y
...
>>> @dataclass
... class B(A):
...     m: int
...     n: InitVar[int]
...     mn: int = field(init=False)
...
...     def __post_init__(self, y: int, n: int) -> None:
...         super().__post_init__(y)
...         self.mn = self.m * n
...
>>> b = B(x=2, y=4, m=3, n=6)
>>> b
B(x=2, xy=8, m=3, mn=18)
History
Date User Action Args
2021-06-11 02:17:40MicaelJarniacsetrecipients: + MicaelJarniac, eric.smith, docs@python
2021-06-11 02:17:40MicaelJarniacsetmessageid: <1623377860.89.0.382005265925.issue44365@roundup.psfhosted.org>
2021-06-11 02:17:40MicaelJarniaclinkissue44365 messages
2021-06-11 02:17:40MicaelJarniaccreate