python中的__slot__

来源:互联网 发布:职业蛙 知乎 编辑:程序博客网 时间:2024/05/17 08:49

By default, instances of both old and new-style classes have a dictionary for attribute storage. This wastes space for objects having very few instance variables. The space consumption can become acute when creating large numbers of instances.

很多老的和新式的类的实例有一个字典,里面存储了很多属性。对于有限的变量的实例,这就浪费了很多空间。这些空间消耗随着创建实例的增多可以一直积累。

The default can be overridden by defining __slots__ in a new-style class definition. The __slots__ declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because __dict__ is not created for each instance.

这些默认的可以被新的类中__slots__重载,__slot__申明会为每个实例分配足够的空间给一些实例变量,空间被保存,因为__dict__不是为每一个实例创建。

0 0
原创粉丝点击