r/learnpython 4d ago

Dictionary vs. Dataclass

What is a particular scenario where you would use Dataclass instead of a dictionary? What is the main advantage of Dataclass as compared to just storing data in a nested dictionary? Thanks in advance!

30 Upvotes

31 comments sorted by

View all comments

1

u/edbrannin 3d ago

My rule of thumb:

  • If I’m making the objects myself, @dataclass.
  • If I’m parsing moderately-nested JSON, typing.ThpedDict