Loading topic
__init__, know the difference between instance and class attributes, and are comfortable with dictionaries. By default, every instance stores its attributes in a per-instance __dict__. Declaring __slots__ tells Python to reserve a fixed set of a3 min read__init__, that you can tell instance attributes from class attributes, and that you have seen __slots__ declared on a class. Here you will see what code can no longer do once a class declares __slots__.3 min read__slots__ is declared and what it prevents: new attributes and the instance __dict__. It also assumes you know what a class attribute is. Here you will see the mechanism behind that behavior. Slots are implemented with descriptors stored on the class.2 min read__dict__ works and how __slots__ is declared, both covered in earlier chapters. Here you will measure what slots save, see where the usual measurements mislead, and learn to report a result without overstating it.3 min read__init__, the difference between instance and class attributes, and how a slot descriptor sits on the class (covered in the previous chapters). Here you will see why a default value cannot share a name with a slot, and what to do instead.3 min read__slots__ removes the instance __dict__ and how basic subclassing works. It covers what happens to that saving when you inherit from a slotted class.3 min read__slots__ removes the instance __dict__, how slot descriptors work, and how subclasses inherit slots. It covers two special names you can list in __slots__: __weakref__ and __dict__.2 min read__slots__ removes the instance __dict__, why a class attribute with the same name as a slot raises ValueError, and the basics of @dataclass. It also assumes you know how slots behave under inheritance.3 min read__slots__ removes the instance __dict__ (earlier chapters), and that you have used pickle and copy at a basic level. The question here is how Python saves and duplicates an object when there is no __dict__ to read its state from.2 min read__dict__, how __slots__ replaces that dict, and the main side effects: no new attributes, class-variable conflicts, and inheritance rules. Here you will turn those facts into a decision.3 min read