Skip to content

Commit

Permalink
Fix duplicate sentences, malformatted example in Post-init processing…
Browse files Browse the repository at this point in the history
… section
  • Loading branch information
akabraham committed Nov 12, 2024
1 parent e33b6fc commit f9f6ec5
Showing 1 changed file with 11 additions and 15 deletions.
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

0 comments on commit f9f6ec5

Please sign in to comment.