3 ms·
The 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
by dec0dedab0de 4y ago
The 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.