Loading topic
self.x = 1 in a method, Python has to put the value 1 somewhere and remember that it belongs to the name x on that one object. In an ordinary class, that place is a dictionary attached to the instance. This chapter explains how that dictionary works and how it interacts with cla8 min read__slots__ and what the declaration does to a class. It assumes you can write classes with __init__, know the difference between instance and class attributes, and know how __dict__ stores attributes by default. The previous chapter covered that default storage6 min read__slots__. It assumes you can write classes with __init__, you know the difference between instance and class attributes, and you know that a regular instance stores its attributes in a dictionary. The previous 5 min read__slots__. It assumes you know the descriptor protocol at a basic level (__get__, __set__, __delete__) and the difference between instance and class attributes. You should also have read the earlier chapters on __dict__ storage and on declaring `_5 min read__dict__ and how __slots__ replaces that dict with fixed slots. You should also be comfortable running short scripts and reading numeric output. The aim is to measure what slots save in a way you can trust, and to avoid claim7 min read__slots__ declared on a class. You should also know that slot names become descriptors on the class, which the previous chapter covered. Here you will see why the usual "default value as a class attribut6 min read__init__, and that you know how a plain instance stores attributes in its __dict__. It also assumes you know how __slots__ fixes an instance's attributes and removes that dict, and that you are comfortable with single and multiple inheritance. T6 min read__slots__ removes from an instance. You should also know that a regular instance stores attributes in __dict__. Here you will see two things a slotted class gives up without saying so: weak references and the ability to add arbitr6 min read__slots__ replaces the instance __dict__. It also assumes you know why a class attribute that shares a name with a slot raises ValueError, and how slots behave under inheritance. You should be comfortable with the basics of @dataclass: annotated fields, defa6 min read__slots__ removes the per-instance __dict__, and that you have used pickle and copy on ordinary classes. A slotted object has no __dict__ to serialize. This chapter explains what pickle and copy do instead, where that fails, and how to take control with 5 min read__slots__. It assumes you have read the earlier chapters. You should know how the instance __dict__ works, what member descriptors are, how slots interact with inheritance, and how to add __weakref__. It also assumes you can read bas7 min read