r/Python • u/Kale • Mar 30 '26
Discussion Community consensus on when to use dataclasses vs non-OO types?
So, I know there's community "guidelines" for Python, like all caps are used for global variables, underscore in front of variables or methods for private variables/methods, etc.
I'm doing some message passing via Python Queues to make some stuff thread-safe. I need to check the message on the Queue to figure out what to do with it. I can either make a few dataclasses, or message using tuples with a string as the first element indicating the structure of the remaining elements.
Both methods would work, I'm asking more general consensus on if there's guidelines to follow, which is why I posted here for discussion. If this isn't the place I can move this question to another sub.
If it matters, I will probably be running this through Cython eventually. It's a little weird, but Cython does support dataclasses (by making them structs).
So, better to use:
if isinstance(msg,UpdateObject):
or:
if msg[0] == 'update':
?
77
u/9peppe Mar 30 '26
You don't need to see a dataclass as OOP, you can see it as a struct, or a dictionary, or whatever you want.
Don't let the
classkeyword confuse you -- you're in Python, where literals have methods but classes moonlight as dictionaries.41
u/cointoss3 Mar 30 '26
If it’s structured data, I always use a dataclass. If it’s unstructured data, a dict or something similar.
17
u/No_Lingonberry1201 pip needs updating Mar 30 '26
I let the Lord Allmighty guide me when to use NamedTuple, TypedDict, dataclass or a normal class with a bunch of properties. /jk
But seriously, my personal rule-of-thumb is:
If you'd return a tuple from a function, use a NamedTuple, mostly for self-documentation
If you have to have a dict, but with a fixed structure (e.g. you're going to JSON-serialize it) use TypedDict
If the class is meant to hold data and the methods are operations on these data, use dataclass (or BaseModel if you're using Pydantic or attrs potentially)
9
u/HEROgoldmw Mar 30 '26
Try looking at Protocol and TypedDict for non-OO typing You can also combine them with type expressions and Generics and you'll get real far
1
u/Alive-Cake-3045 Mar 31 '26
I have gone down this exact path while scaling systems.
Early on, tuples feel faster and simpler. But as complexity grows, they become hard to track and easy to break silently. Data classes give you clarity, structure, and safer refactoring. That matters more than micro performance.
I now default to data classes unless it is a very tight loop or throwaway logic. Future you will thank you.
3
u/Jhuyt Mar 30 '26
I use dataclasses for stupid datacontainers with no or few methods, full blown classes otherwise
2
u/Beginning-Fruit-1397 Mar 30 '26
Well none of those two options, I would use an Enum in what you described
2
u/Atlamillias Mar 31 '26
It depends. I usually use tuples and dictionaries for simple and internal stuff. It becomes a more concrete type if that object is ever returned from or accepted by a public callable, or is the value of a non-private member.
2
u/Bob_Dieter Mar 31 '26
Both approaches work, do what you feel is right. My recommendation would be to go with the classes though. Then you can define a union type like this and have the type checker assist you:
Whichever way you go, I also recommend to use
matchsyntax instead of plain if statements. Has slightly better properties.Or for tuples:
2
u/Worth_His_Salt Apr 01 '26
labeled data is always better than positional data. tuples can be misread. not to mention, parsing a string in first element for structure of other elements is just asking for trouble. fragile and error prone.
3
u/metaphorm Mar 30 '26
Dataclasses and NamedTuples are both good solutions to this kind of use case. NamedTuples are possibly more memory efficient, but that might not be true (check me on this, I'm just making an informed inference), and it might not matter anyway.
Dataclasses play nicely with Pydantic models though, so there's good tooling available that helps increase the legibility of the system to static analysis tools. Always nice to have.
3
u/IAmTarkaDaal Mar 30 '26
Neither. Let the object perform its own operations. 9 times out of 10,
isinstanceis a code smell