5 ms·
I still don't understand why you would use classes without methods. That's what dicts are for. About 10 years ago someone who I thought was pretty smart said i
by dec0dedab0de 4y ago
I still don't understand why you would use classes without methods. That's what dicts are for.
About 10 years ago someone who I thought was pretty smart said it was because dots are nicer to type, fair enough I guess. It just always seemed silly to me to go through setting up a class but not getting the benefits of using methods.
- bl0rg 4y ago1. Dots are nicer to type for sure 2. It's a bit easier to define types for class attributes than dictionaries IMO 3. You can absolutely have methods on dataclasses
- Liquid_Fire 4y agoThere is clear value in being able to pass around several pieces of information about the same thing without having to individually name them every time (less typing, you can decide to include more members at a later time, less overhead in passing by reference, users can treat it as an opaque type without needing to know the contents, etc).
- thanatropism 4y agoThis. I look at old code I wrote and cringe at things like def foo(...): ... return yhat, sse, aic, bic yhat, _, _, _ = foo(ytrue) plot(ytrue, yhat)
- eru 4y agoDicts are much more annoying to give types for. They are also slower.
- st4lz 4y agoThere is a slight distinction between a dictionary and a class. Dict is a mutable key-value storage, so can be used as a collection of objects (or values) and benefit from its dynamism. Dataclasses are meant to define an object, so the emphasis is on clearly declaring the purpose of it upfront, enhancing readability.
- sakex 4y agoDocumenting your type.
- evomassiny 4y agodataclasses document the full set of attribute they can hold, along with their type.
- thanatropism 4y agoFuture-proofed, self-documenting code?
- zzzeek 4y agoclasses themselves are types, which is semantically valuable and also provides for a straightforward inheritance pattern. attributes on classes are easier to type. There's pep-589 TypedDict but it's not very easy to use right now.
- beiller 4y agoI'm in the same boat. I've been using TypedDict all over the place and it's quite nice! The only plus side to dots I had was the IDE auto-filling it, and my IDE seems to work just fine with TypedDict.
- teddyh 4y agoClasses can change an attribute to a property while keeping the interface intact. Like turning an SQL table into an updateable view.
- BeFlatXIII 4y agoIDE autocompletion is better with classes than with dicts.