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?
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.
From Zen of Python:
> There should be one--and preferably only one--obvious way to do it