I've been learning OOP but the dataclass decorator's use case sort of escapes me.
I understand classes and methods superficially but I quite don't understand how it differs from just creating a regular class. What's the advantage of using a dataclass?
How does it work and what is it for? (ELI5, please!)
My use case would be a collection of constants. I was wondering if I should be using dataclasses...
class MyCreatures:
T_REX_CALLNAME = "t-rex"
T_REX_RESPONSE = "The awesome king of Dinosaurs!"
PTERODACTYL_CALLNAME = "pterodactyl"
PTERODACTYL_RESPONSE = "The flying Menace!"
...
def check_dino():
name = input("Please give a dinosaur: ")
if name == MyCreature.T_REX_CALLNAME:
print(MyCreatures.T_REX_RESPONSE)
if name = ...Halp?
Videos
I write classes quite frequently for various data structures (eg Bloom filters) but I had never heard of dataclass until recently. Is that now the recommended way to write classes in Python?
As I've gotten comfortable with dataclasses, I've started stretching the limits of how they're conventionally meant to be used. Except for a few rarely relevant scenarios, they provide feature-parity with regular classes, and they provide a strictly-nicer developer experience IMO. All the things they do intended to clean up a 20-property, methodless class also apply to a 3-input class with methods.
E.g. Why ever write something like the top when the bottom arguably reads cleaner, gives a better type hint, and provides a better default __repr__?
As the title says, I've read the whole documentation more than once and have used them plenty of times. But in practice for me it's just a way to avoid writing an init method. This is a good QL improvement, I guess, but now means that my code uses another dependency and extra complexity for what? I won't argue the merits of options like hash and frozen, but i don't find myself needing that functionality that often. So I return to my genuine question, why has the community so quickly adopted and recommended the use of dataclasses? I'm referring to articles and YouTube videos about them.
In what use cases are you finding success with them?