Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

10
  • 23
    The dataclass module is new in Python 3.7 but you can pip install dataclasses. It's the backport on Python 3.6. pypi.org/project/dataclasses/#description Commented May 21, 2018 at 23:50
  • 2
    +1 for improved NamedTuple declaration. The old way was really unpleasant to read if you had several variables... Commented Jan 28, 2019 at 11:07
  • 2
    @PurpleIce It was an implementation of PEP 557, Data Classes @dataclass The details are here: pypi.org/project/dataclasses/#description Commented Feb 19, 2019 at 4:30
  • 5
    The first example can also create an immutable data class by using @dataclass(frozen=True). Then you can use instances as dictionary keys, set members, etc., e.g., {Point(1, 2): 'a location'}. Commented Mar 30, 2021 at 20:33
  • 3
    I'd suggest adding KW_ONLY like this @dataclass(kw_only=True) to mark all fields as keyword-only Commented Aug 1, 2023 at 16:08

lang-py