Python __slots__ and object layout explained

preview_player
Показать описание
Save memory using Python slots.

Python __slots__ are useful for saving memory when you have a large number of small objects whose attributes are known ahead of time. In this video we learn about __slots__, what slots actually are and how they work.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, John Martin, Casey G, Pieter G, Krisztian M, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
0:43 Normal class
1:53 Slots class
2:44 Why use slots
4:21 Slots are descriptors
5:45 What are slots
8:32 Inheritance
9:14 No metaclass slots
9:24 Using a dict slot
Рекомендации по теме
Комментарии
Автор

“I’d like to thank me for sponsoring myself” 🤣🤣🤣🤣🤣

AWriterWandering
Автор

It's not just the memory savings. Restricting the ability to add new members is a feature itself.

If I make a typo when assigning to an attribute, I want to get an error. I don't want it to be silently ignored, leaving me wondering why the value doesn't change.
That is really the main reason I always use slots. Memory savings are just a nice side-effect.

Yotanido
Автор

The way you pick examples is phenomenally efficient at highlighting limitations and advantages of whatever functionality you present.

alcyonae
Автор

this channel is literally a gem im glad i found you

strandingstranger
Автор

The reason to include __dict__ in __slots__ is because it makes it much easier to extend/modify the code, while _still_ giving you memory savings over the default.

Anything stored in the __dict__ incurs additional storage overhead not just because of the dictionary headers, but also for (a) storing the _key_, and (b) due to the additional empty nodes needed to make a hash table efficient --averaging about 3 pointers worth of storage per entry, while a slot variable always only takes one.
Therefore, any variable you can move into a slot represents a decrease in memory footprint, even if you can't move all of your variables into slots and still need __dict__.

Plus, current versions of python don't actually create the underlying dictionary object that goes in __dict__ until a non-slot variable on the object is written to, so depending on the usage pattern of your code, it could actually be _more_ memory efficient to leave a set of variables that only a small fraction of the objects of some class will use out of __slots__ and just rely on the automatic creation of a __dict__ in those cases.

AJMansfield
Автор

no useless words in the content, all are knowledge intensive explanation. perfect!

mike
Автор

5:51 "We need to enter the matrix"
Wouldn't this be more like leaving the matrix because you're peeking behind the curtain?

goeiecool
Автор

Love it! As a C / C++ dev (in past) I can appreciate this. However as u mentioned we are rarely memory constrained. Thx !

rickharold
Автор

Thank you, your explanation was awesome! I've been doing Python for ages but never really bothered with __slots__. At least now, I know what they are, their trade-offs and what they're used for.

Roarshark
Автор

I'm working as a data scientist for 3 years using python with no-CS background and the topic you cover is always interesting and mindblowing.

vinson
Автор

Nice. Your content is unique and not found anywhere else. Keep up the good work!

phuctran
Автор

There's so much information, my brain is frying. I'll be looking back at your videos constantly!🤯

MaxTechniche
Автор

I find not being able to add a member actually a very interesting feature. This is something I often don't want to do, but can happen by accident. Of course, mypy can catch that often.

n-ds
Автор

I would also like to thank you for sponsoring yourself.

davea
Автор

Thanks, now i actually understand the previous video completely

laurinneff
Автор

“I’d like to thank me, for sponsoring myself.”

NStripleseven
Автор

That's the best sponsor spot ever!

AsgerJon
Автор

Its crazy how all your videos are intresting and easy to follow!

ipbody
Автор

great video!
I would like to see more c stuff, maybe even showing stuff on how python code translates to c code

טלאיצקוביץ-במ
Автор

Really good video, as always. One request: can you do a video where you talk about making the Python classes in C? I use the CPython a lot to make modules (with only functions) in order to make faster codes. I was trying to make the Python classes directly from the C API but I have a lot of trouble. Thanks!

Mustak