Hacker News new | ask | show | jobs
by the__alchemist 317 days ago
All good ones. I'll add this because A: It's common in Python, and B: There are suitable alternatives in the standard library:

Conflating key/value lookups (dicts) with structured data (classes). They are both useful tools, but are for different purposes. Many python programmers (Myself many years ago included!) misused dicts when they should have been using dataclasses.

2 comments

Everything in the codebase I maintain at my job is an arbitrary dict and there is no type information anywhere. It wasn't even written that long ago (dataclasses were a thing long before this codebase was written).

There's actually a place where the original authors subclassed dict, and dynamically generate attributes of a "data class" such that it can be used with dotted attribute access syntax or dict access syntax but the `__slots__` attribute of these classes is also generated dynamically so you don't have any auto-complete when trying the dotted attribute access. It's genuinely insane lol.

Dataclasses are relatively recent addition to the language: dicts were the most reasonable way to do it in the past, especially before type checking.

Under the hood, classes are also dict lookups, so really, this is mostly about adding type checking (unless you are also using slots).