3 ms·
Maybe I didn't have enough coffee, but what's really the point of "data" keyword? Seems like its only goal is to confuse. So you write that data thingy and late
by varispeed 4y ago
Maybe I didn't have enough coffee, but what's really the point of "data" keyword?
Seems like its only goal is to confuse.
So you write that data thingy and later figure out you actually need a class, then what?
What's the difference between data and class? Because of what it keeps?
So why not have "methods" for classes that only have methods or "notdecided" if you don't yet know what sort of class you are going to end up with.
I don't follow.
- dec0dedab0de 4y agoThe idea is that dataclasses are for when you just want to store some data in a pre-structured format. It is almost the same thing as a named tuple. It trades away the flexibility of processing the incoming data in __init__ to make defining the class simpler. I agree that the name is confusing, I think maybe @autoinit would have been better.
- varispeed 4y agoWhy Python interpreter can't figure it out like "oh this class only stores data, so it must be a data class". No need for decorators etc. What would be the consequences of non-data class that only contains data to treat is as a data class? Likely none.
- dec0dedab0de 4y agoData classes are just regular classes but with some extra syntactic sugar(magic) to automate some common stuff.
- kortex 4y agoGood luck introspecting a given python object and ascertaining "this class only stores data". It's literally impossible given the dynamicism of python. The consequences are exposing state that you don't necessarily want to expose, missed opportunities for optimization (lose fine-grained memory/performance tradeoff ability), imply something is serializable when it isn't, collisions with metaclasses/inheritance weirdness.
- thanatropism 4y agoDataclasses already exist and have throngs of users. It's for them. If you've never used dataclasses maybe it doesn't make sense to you. Then, I agree Python is way too big a language now, but I'd rather start removing things like the walrus and operator overloading.
- kortex 4y agoOperator overloading is what makes the majority of science/math heavy libraries (numpy, scipy, pandas, torch, scikit) work. It ain't going anywhere. Walrus is fine. It's easy enough to google "python colon equals" and figure it out.