Software Bits Newsletter

Share this post
Python slots.
softwarebits.substack.com

Python slots.

Or how to leverage constraints.

Taras Tsugrii
Oct 23, 2021
Comment2
Share

Dynamic programming languages are popular when it comes to prototyping thanks to REPL and ease with which most of the things, including class schema, can be changed at runtime. But we all know that everything has a cost and this dynamism is not an exception. For example, for a simple class below

the name attribute value is stored in a __dict__ under the hood

This means that the attribute value accesses result in dictionary lookups and dictionary instance takes significantly more memory than a string.

But what if we don’t need this extra flexibility? Well, that’s where slots come into picture. We can use slots to define all attributes in advance

In this case, instead of using __dict__ for attribute lookups, a pointer to a name attribute will be stored directly in the User2 instance, which results in reduced memory footprint

since __dict__ and __weakref__ attributes are gone and name attribute is stored directly in the u2 instance

Great, looks like we’ve managed to reduce memory usage, but what about performance? Does it even matter? Let’s find out.

So looks like we also get ~16% boost for attribute lookups for free. The only disadvantage is a somewhat ugly syntax and it would be great to use typing.NamedTuple that serves the same purpose as good old collections.namedtuple but with a much better aesthetics

Unfortunately even though it doesn’t use __dict__, it also does not use __slots__ and as such has performance similar to dict based User1

So to get the best possible memory and performance characteristics, explicit usage of slots is a small price to pay and as such should be considered for compute and memory intensive applications, although in such case usage of other programming languages may be a better idea :)

Comment2
ShareShare

Create your profile

0 subscriptions will be displayed on your profile (edit)

Skip for now

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.

Q QQ
Oct 24, 2021

Try a datastruct instead of a namedtuple

Expand full comment
Reply
1 reply by Taras Tsugrii
1 more comments…
TopNewCommunity

No posts

Ready for more?

© 2022 Taras Tsugrii
Privacy ∙ Terms ∙ Collection notice
Publish on Substack Get the app
Substack is the home for great writing