Hacker News new | past | comments | ask | show | jobs | submit login

I think for the same reason you should avoid TypedDicts for new APIs as well. Dataclasses are the natural replacement for both.





Not really. A lot of tooling, JSON for example, naturally works with dictionaries. A TypedDict naturally connects with all those tools. In contrast, dataclasses are hostile to the enormous ecosystem of tools that work with dictionaries.

If you store all your data is dataclasses, you end-up having to either convert back to dictionaries or having to rebuild all that tooling. Python's abstract syntax trees are an example. If nodes had been represented with native dictionaries, then pprint would work right out the box. But with every node being its own class, a custom pretty printer is needed.

Dataclasses are cool but people should have a strong preference for Python's native types: list, tuple, dict, and set. Those work with just about everything. In contrast, a new dataclass is opaque and doesn't work with any existing tooling.


An advantage of dataclasses over dicts is that you can add methods and properties.

Also you can easily convert a dataclass to a dict with dataclasses.asdict. Not so easy to go from dict to dataclass though




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: