Skip to content
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

gh-126462: Remove duplicate sentences, fix malformatted example in dataclasses "Post-init processing" section #126463

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions Doc/library/dataclasses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -504,30 +504,26 @@ Module contents
Post-init processing
--------------------

The generated :meth:`~object.__init__` code will call a method named
:meth:`!__post_init__`, if :meth:`!__post_init__` is defined on the
class. It will normally be called as ``self.__post_init__()``.
However, if any ``InitVar`` fields are defined, they will also be
passed to :meth:`!__post_init__` in the order they were defined in the
class. If no :meth:`~object.__init__` method is generated, then
:meth:`!__post_init__` will not automatically be called.
.. function:: __post_init__()

When defined on the class, it will be called by the generated
:meth:`~object.__init__`, normally as ``self.__post_init__()``.
:meth:`~object.__init__`, normally as :meth:`!self.__post_init__`.
However, if any ``InitVar`` fields are defined, they will also be
passed to :meth:`!__post_init__` in the order they were defined in the
class. If no :meth:`!__init__` method is generated, then
:meth:`!__post_init__` will not automatically be called.

@dataclass
class C:
Among other uses, this allows for initializing field values that
depend on one or more other fields. For example::

a: float
b: float
c: float = field(init=False)
@dataclass
class C:
a: float
b: float
c: float = field(init=False)

def __post_init__(self):
self.c = self.a + self.b
def __post_init__(self):
self.c = self.a + self.b

The :meth:`~object.__init__` method generated by :func:`@dataclass <dataclass>` does not call base
class :meth:`!__init__` methods. If the base class has an :meth:`!__init__` method
Expand Down