From 894d09ccd375f2aa7ca73354d71fd72c824444d1 Mon Sep 17 00:00:00 2001 From: Andrew Abraham Date: Tue, 5 Nov 2024 19:01:19 -0500 Subject: [PATCH] Fix duplicate sentences, malformatted example in Post-init processing section --- Doc/library/dataclasses.rst | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 7b09fd2d6e876f1..21a715ec4e94d78 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -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 ` does not call base class :meth:`!__init__` methods. If the base class has an :meth:`!__init__` method