In the previous post
we’ve discussed how it’s possible to reduce memory and improve performance using slots. The disadvantage of this approach is a slightly verbose and non-intuitive syntax, which was not big enough to dissuade us from using it. Fortunately, Python 3.7 introduced dataclasses.dataclass decorator, which looks very promising, so let’s give it a shot:
Hm, something looks wrong - looks like it’s still using __dict__ attribute, takes extra memory and comes with lookup performance overhead :(
But after taking a second look at its list of keywords, it seems like there is a slots keyword that sounds exactly like what we are searching for.
And… finally success!
We’ve finally got to the same memory footprint and even slightly faster lookup performance than our manually written slots implementation! And all of this comes in a concise and intuitive package! As such from now on, there is no excuse to not take advantage of slots power!