Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a21b3d2

Browse files
authored
More clarification of kw_only args. (GH-25838)
Also, clarify that the dataclass decorator is what raises an error for some mutable defaults.
1 parent c59baa0 commit a21b3d2

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

Doc/library/dataclasses.rst

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ Module contents
307307
- ``type``: The type of the field.
308308

309309
- ``default``, ``default_factory``, ``init``, ``repr``, ``hash``,
310-
``compare``, and ``metadata`` have the identical meaning and
311-
values as they do in the :func:`field` declaration.
310+
``compare``, ``metadata``, and ``kw_only`` have the identical
311+
meaning and values as they do in the :func:`field` declaration.
312312

313313
Other attributes may exist, but they are private and must not be
314314
inspected or relied on.
@@ -459,6 +459,9 @@ Module contents
459459

460460
p = Point(0, y=1.5, z=2.0)
461461

462+
In a single dataclass, it is an error to specify more than one
463+
field whose type is :const:`KW_ONLY`.
464+
462465
.. exception:: FrozenInstanceError
463466

464467
Raised when an implicitly defined :meth:`__setattr__` or
@@ -582,9 +585,12 @@ Re-ordering of keyword-only parameters in :meth:`__init__`
582585

583586
After the parameters needed for :meth:`__init__` are computed, any
584587
keyword-only parameters are moved to come after all regular
585-
(non-keyword-only) parameters. In this example, ``Base.y``,
586-
``Base.w``, and ``D.t`` are keyword-only fields, and ``Base.x`` and
587-
``D.z`` are regular fields::
588+
(non-keyword-only) parameters. This is a requirement of how
589+
keyword-only parameters are implemented in Python: they must come
590+
after non-keyword-only parameters.
591+
592+
In this example, ``Base.y``, ``Base.w``, and ``D.t`` are keyword-only
593+
fields, and ``Base.x`` and ``D.z`` are regular fields::
588594

589595
@dataclass
590596
class Base:
@@ -666,14 +672,15 @@ Mutable default values
666672
assert D().x is D().x
667673

668674
This has the same issue as the original example using class ``C``.
669-
That is, two instances of class ``D`` that do not specify a value for
670-
``x`` when creating a class instance will share the same copy of
671-
``x``. Because dataclasses just use normal Python class creation
672-
they also share this behavior. There is no general way for Data
673-
Classes to detect this condition. Instead, dataclasses will raise a
674-
:exc:`TypeError` if it detects a default parameter of type ``list``,
675-
``dict``, or ``set``. This is a partial solution, but it does protect
676-
against many common errors.
675+
That is, two instances of class ``D`` that do not specify a value
676+
for ``x`` when creating a class instance will share the same copy
677+
of ``x``. Because dataclasses just use normal Python class
678+
creation they also share this behavior. There is no general way
679+
for Data Classes to detect this condition. Instead, the
680+
:func:`dataclass` decorator will raise a :exc:`TypeError` if it
681+
detects a default parameter of type ``list``, ``dict``, or ``set``.
682+
This is a partial solution, but it does protect against many common
683+
errors.
677684

678685
Using default factory functions is a way to create new instances of
679686
mutable types as default values for fields::

0 commit comments

Comments
 (0)