Use keyword-only arguments in Python dataclasses

28 points by ubernostrum


munyari

Another great dataclass parameter is frozen=True, which marks your dataclasses as immutable. Hopefully the benefits of immutability speak for themselves, but it also makes your dataclass hashable for free, allowing you to use them in sets/as keys dict keys.

Halkcyon

I used to be annoyed by keyword-only in Pydantic and debated changing the default for a long time, but I’ve come around on wanting keyword-only on any class construction because it’s explicit and easy to change which is more valuable to me now than making the code shorter.

Aks

This is such a good tip, thanks for sharing. Some of my projects could have used this. I admit I still don’t know much of the @annotation system in Python.

Is there something else like this, especially for creating enum classes?