News for hackers. Stuff that matters.
Re:Don't let dicts spoil your code (2020) by throwaway894345 on Sunday June 26, 2022 @01:18AM (#31881323)
This is one of the things I appreciate about languages like Go and Rust (I'm sure there are others as well). If the data is static, use a struct. If the data is dynamic, use a map/HashMap. No need to worry about TypedDict vs classes vs DataClasses vs etc, and no one uses HashMap for static data (they could, but virtually no one in those communities is such a glutton for punishment).

From Zen of Python:

> There should be one--and preferably only one--obvious way to do it

Re:Don't let dicts spoil your code (2020) by mathisonturing on Sunday June 26, 2022 @01:35AM (#31881398)
Forget about DataClasses, TypedDict etc. Can't you achieve the same in python with a class and a dict? Is there a difference, other than perhaps being overloaded with options?
Re:Don't let dicts spoil your code (2020) by xigoi on Sunday June 26, 2022 @04:06AM (#31882020)
A dataclass will get things like __repr__ for free.
Re:Don't let dicts spoil your code (2020) by PartiallyTyped on Sunday June 26, 2022 @04:07AM (#31882027)
Eq, hash, and init is also "free".
Re:Don't let dicts spoil your code (2020) by throwaway894345 on Sunday June 26, 2022 @05:33PM (#31888218)
There’s also implementation differences. Accessing attributes and methods on a regular class may be slower because (IIRC) it has to do a lookup on each instance’s dict, whereas I believe dataclass implementation is more optimized (I’ve clearly forgotten the details).
Re:Don't let dicts spoil your code (2020) by bottled_poe on Sunday June 26, 2022 @09:01AM (#31883509)
Python is one of those languages where everything starts to look like a nail.
Re:Don't let dicts spoil your code (2020) by throwaway894345 on Sunday June 26, 2022 @05:35PM (#31888243)
Yeah, I’ve programmed in Python professionally for about a decade and I used it for hobby stuff for about 5 years before that. I still don’t feel like I’ve mastered it, whereas I felt I had mastered Go after about ~2 years. I think Go implements Python’s “zen” a lot better than Python does.