Micael Jarniac <mic...@jarniac.com> added the comment:

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)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44365>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to