-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-43558: Add base class initialization to dataclass doc #25967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -491,6 +491,27 @@ depend on one or more other fields. For example:: | |
| def __post_init__(self): | ||
| self.c = self.a + self.b | ||
|
|
||
| The :meth:`__init__` method generated by :func:`dataclass` does not call base | ||
| class :meth:`__init__` methods. If the base class has an :meth:`__init__` method | ||
| that has to be called, it is common to call this method in a | ||
| :meth:`__post_init__` method:: | ||
|
|
||
| @dataclass | ||
| class Rectangle: | ||
| height: float | ||
| width: float | ||
|
|
||
| @dataclass | ||
| class Square(Rectangle): | ||
| side: float | ||
|
|
||
| def __post_init__(self): | ||
| super().__init__(self.side, self.side) | ||
|
|
||
| Note, however, that in general the dataclass-generated :meth:`__init__` methods | ||
| don't need to be called, since the derived dataclass will take care of | ||
| initializing all fields of any base class that is a dataclass itself. | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good. How about instead of "the derived class", use "the derived dataclass"? I'm trying to draw the distinction that if a non-dataclass is derived from a dataclass, then the base class's
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand PEP 557 correctly, the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If a dataclass inherits from a non-dataclass, then it's probably that the base non-dataclass's If a non-dataclass inherits from a dataclass, then the non-dataclass most likely does need to call the dataclass's It's only the case where a dataclass inherits from a dataclass that the base class's
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very helpful! Thank you! |
||
| See the section below on init-only variables for ways to pass | ||
| parameters to :meth:`__post_init__`. Also see the warning about how | ||
| :func:`replace` handles ``init=False`` fields. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add the remark to :mod:`dataclasses` documentation that the :meth:`__init__` of any base class | ||
| has to be called in :meth:`__post_init__`, along with a code example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great example.