Couple weeks ago I learned about dataclasses
feature in Python. This feature is a nice way to define classes in python. This was introduced in Python 3.7, in particular PEP 557. To use this feature make sure you are running python version 3.7 or above.
Previously, I knew about defining new classes in python using the self.__init__()
function. Here's an example of me defining a class Youtuber
.
class Youtuber:
def __init__(self, name: str, categories: list[str]):
self.name = name
self.categories = categories
Now we can use less boilerplate code to define classes. There is no need to install a separate python library, this will come with python standard library (as long as it's > 3.7).
Here's how you can define these now:
from dataclasses import dataclass
@dataclass
class Youtuber:
"""Class for defining youtubers."""
name: str
categories: list[str]
In this example above, I am importing it first from dataclasses import dataclass
and then defining it. I created a class definition and used @dataclass
annotation to tell python how it should interpret this class definition.
Then I can create this in python, as I would do it anyway, regardless of using @dataclass
:
Youtuber("Chris Stuckmann", ["movie-reviews"])
Here's how you can try this out, using 3 of my favorite youtube channels :)
youtubers = []
youtubers.append(Youtuber("Chris Stuckmann", ["movie-reviews"]))
youtubers.append(Youtuber("Double Toasted", ["movie-reviews"]))
youtubers.append(Youtuber("The Fitness Marshall", ["fitness"]))
for youtuber in youtubers:
print(f"{youtuber.name}'s categories are: {youtuber.categories}")
Let's put this all together in a file called youtubers.py
and run it! You can copy it from this GitHub gist I created. You should get this output:
$ python youtubers.py
Chris Stuckmann's categories are: ['movie-reviews']
Double Toasted's categories are: ['movie-reviews']
The Fitness Marshall's categories are: ['fitness']
Hope you enjoyed this short article!
You can find me on https://isabelcosta.github.io/
Top comments (6)
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 🤗
Thanks for the information! I will certainly use this in the future.
FYI: you can use repl.it to share the code in a place that it can be executed by others without having to copy into their local.
Nice suggestion! Thank you, Corey :)