__slots__为什么能节省内存?

来源:互联网 发布:培训机构股权分配 知乎 编辑:程序博客网 时间:2024/04/30 17:33

注:本文是对http://www.datadependence.com/2016/07/pythonic-code-video-series-slots/ 的翻译。说是翻译,但不会逐字逐句翻译。宗旨是回答标题的问题,所以也会增加自己的理解。
很多人都看过Oyster.com的这篇相当当的文章:
SAVING 9 GB OF RAM WITH PYTHON’S _SLOTS_
为了能够理解_slots_,最好的方式是先了解域是如何关联python类的。考虑下面这个类:

 class Measurement:     def __init__(self,x,y,value):         self.x = x         self.y = y         self.val = value

假如我们创建少量几个这个类的实体,通过初始化方法设置值,并给一个实体动态增加属性及值,如下:

m1 = Measurement(1,2,"Happy")m2 = Measurement(7,10,"Crazy")

那在内存中的存储大概类似如下:
这里写图片描述

从上图可以看到,每个实体都有指向一个包括属性名及属性值的_dict_。我们可以打印出来:

print(m1.__dict__) #{'x':1,'y':2,'val':'happy'}print(m2.__dict__) #{'x':7,'y':10,'other':True,'val':'Crazy'}

如果我们不是像m2.other = True那样在类创建好后定制属性。并且,我们要创建非常非常多的实例(比如,百万级),那这种方式就是非常低效的——我们需要在内存中存储对应实体数量包含重复键的字典。

使用_slots_

只要简单的修改下Measurement类的属性的存储就能消除重复,得到一个1比1的字典分配。

class Measurement:    __slots__ = ['x','y','val']    def __init__(self,x,y,value):        self.x = x        self.y = y        self.val = value

(译者注:原文是在python3上实现的。因为python3只有新式类,无需任何指定。但是如果是在python2(译者熟悉的2.6/2.7)上。上面的写法就是错误的,具体看下代码,运行后就知道了)

class Measurement(object):    __slots__ = ['x','y','val']    def __init__(self,x,y,value):        self.x = x        self.y = y        self.val = valueclass Measurement_old:    __slots__ = ['x','y','val']    def __init__(self,x,y,value):        self.x = x        self.y = y        self.val = valuem1 = Measurement(1,2,"new")m2 = Measurement_old(2,3,"old")print dir(Measurement)print dir(Measurement_old)print type(Measurement.x) #<type 'member_descriptor'>#print m1.__dict__ #AttributeErrorprint m2.__dict__ #{'y': 3, 'x': 2, 'val': 'old'}

这样,内存中的存储就大概类似这样的了:
这里写图片描述

现在从图可以看出,属性名和Measurement类关联,而不再是和它的实体关联。好处是显而易见的。

1 0