python slot
每个实例包含一个字典,slot 让实例变成tup 或list,减少内存,但不能再增加属性
For classes that primarily serve as simple data structures, you can often greatly reduce the memory footprint of instances by adding the slots attribute to the class defi‐ nition. For example:
class Date:
slots = ['year', 'month', 'day'] def init(self, year, month, day):
self.year = year
self.month = month
self.day = day
When you define slots, Python uses a much more compact internal representation for instances. Instead of each instance consisting of a dictionary, instances are built around a small fixed-sized array, much like a tuple or list. Attribute names listed in the slots specifier are internally mapped to specific indices within this array. A side effect of using slots is that it is no longer possible to add new attributes to instances— you are restricted to only those attribute names listed in the slots specifier