Optimization of Data Classes
00:11 Data classes have no explicit syntax for working with slots, but the normal way of creating them works for data classes as well. You probably expect this as, after all, data classes are really just regular classes.
In essence, slots are defined using the
.__slots__ to list the variables on a class. Variables or attributes not present in the
.__slots__ list may not be defined. Furthermore, a slots class may not have default values.
Similarly, slots classes are typically faster to work with. The following example measures the speed of attribute access on a slots data class and a regular data class using
timeit from the standard library.
Become a Member to join the conversation.