Why not use namedtuples/dictionaries for this purpose? They both have less overhead than constructing dataclasses (especially namedtuple), unless I'm mistaken. namedtuple can be accessed just like this, but is immutable. Dictionaries do have to be accessed via different syntax...
Just not seeing where I would want to use dataclasses vs other formats. If you could hint at what the nice use cases vs these others could be, that would be greatly appreciated!
I'm a Software Engineer, from Portugal, a GitHub Star and a mentor at Black CodHer Bootcamp and "As Raparigas de Codigo" organization. Previously, I've been an admin at AnitaB.org Open Source.
Interesting point, Christopher! The example I used here is quite simple, for demonstration purposes. I wanted to keep this post short as a small introduction to this feature I found exciting :) I haven't used much of namedtuples yet. I would use this dataclass feature next time I feel like defining a class and avoid write the functions that come built-in with this annotation. I think the official documentation explains it well. I haven't look yet into performance gains, but I've seen there are some posts out there exploring more of the differences of namedtuples vs dataclasses.
Hi, I revisited this now, it seems the primary difference is dataclasses are mutable, while namedtuples are immutable. Aside from that, using dictionaries can work, but has roughly similar overhead to dataclasses; or at least the difference is negligible. It really comes down to whether you would want the ability to traverse all values/ have specific named attributes. Thanks for this article! ^_^
I'm a Software Engineer, from Portugal, a GitHub Star and a mentor at Black CodHer Bootcamp and "As Raparigas de Codigo" organization. Previously, I've been an admin at AnitaB.org Open Source.
Awesome Christopher! It makes much sense now, why this exists :)
Thank you so much for coming back and sharing your knowledge!
I just learned something new with this 🤗
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Why not use namedtuples/dictionaries for this purpose? They both have less overhead than constructing dataclasses (especially namedtuple), unless I'm mistaken. namedtuple can be accessed just like this, but is immutable. Dictionaries do have to be accessed via different syntax...
Just not seeing where I would want to use dataclasses vs other formats. If you could hint at what the nice use cases vs these others could be, that would be greatly appreciated!
Interesting point, Christopher! The example I used here is quite simple, for demonstration purposes. I wanted to keep this post short as a small introduction to this feature I found exciting :) I haven't used much of namedtuples yet. I would use this dataclass feature next time I feel like defining a class and avoid write the functions that come built-in with this annotation. I think the official documentation explains it well. I haven't look yet into performance gains, but I've seen there are some posts out there exploring more of the differences of namedtuples vs dataclasses.
Hi, I revisited this now, it seems the primary difference is dataclasses are mutable, while namedtuples are immutable. Aside from that, using dictionaries can work, but has roughly similar overhead to dataclasses; or at least the difference is negligible. It really comes down to whether you would want the ability to traverse all values/ have specific named attributes. Thanks for this article! ^_^
Awesome Christopher! It makes much sense now, why this exists :)
Thank you so much for coming back and sharing your knowledge!
I just learned something new with this 🤗