Python __slots__

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2024 Feb 29 4:22
Editor
Edited
Edited
2024 Feb 29 5:4
Refs
Refs
if a __slots__ list or tuple is defined, then a class will not have a __dict__ to store arbitrary values, thus saving memory. Instead, only the attributes defined in __slots__ can be set.
__slots__ are halfway between using a namedtuple and a regular class.
 
 
 
 
Python Classes, namedtuples and __slots__ - maurodec.com
Data types are needed in pretty much every program that needs to store and manipulate data. These types usually have little to no behavior and simply exist to keep track of information in a structured and orderly way. Recently, PEP 557 (Data Classes) was accepted, however, it won’t appear until Python 3.7.
Python __slots__ and object layout explained
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. ― mCoding with James Murphy (https://mcoding.io) Source code: https://github.com/mCodingLLC/VideosSampleCode Slots docs: https://docs.python.org/3/reference/datamodel.html#slots Descriptor docs: https://docs.python.org/3/reference/datamodel.html#descriptors SUPPORT ME ⭐ --------------------------------------------------- Patreon: https://patreon.com/mCoding Paypal: https://www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJHEE Other donations: https://mcoding.io/donate Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, John Martin, Casey G, Pieter G, Krisztian M, Sigmanificient BE ACTIVE IN MY COMMUNITY 😄 --------------------------------------------------- Discord: https://discord.gg/Ye9yJtZQuN Github: https://github.com/mCodingLLC/ Reddit: https://www.reddit.com/r/mCoding/ Facebook: https://www.facebook.com/james.mcoding 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
Python __slots__ and object layout explained
 
 

Recommendations