Python中减少内存消耗的方法之一:__slots__属性

Python是一门动态语言,可以在运行过程中,修改实例的属性和增删方法。任何实例都包含一个字典__dict__,该字典保存了实例所有的属性和方法。Python也通过这个字典可以将任意属性绑定到实例上。
有时候在实例的生命周期中处于安全等考虑只能操作固定的属性,不想增加属性,可以通过__slots__来就可以定义一个集合,只有在这个集合中的属性才能被操作。

__slots__ 是一个类的属性,有三个功能:

  1. 实例不能访问或添加__slots__之外的属性
  2. 实例没有__dict__方法
  3. 节省内存,实例保存属性的结构从字典变成列表

不使用__solt__

class A():
    
    t = 30
    def __init__(self,x,y):
        self.x = x
        self.y = y 

    def fun():
        pass 

a = A(100,10)
print(a.__dict__)
print(a.x)
print(a.y)
print(a.t)
>>>
{'x': 100, 'y': 10}
100
10
30

特点:

  1. 可以通过a.__dict__ 输出实例所有属性
  2. 类变量不在a.__dict__管理的范围中

使用__slots__

不能动态添加属性

class A():
    __slots__=('x', "y") 
    def __init__(self,x,y):
        self.x = x
        self.y = y 
a = A(100,10)
print(a.x)
print(a.y)
>>> 
100
10
a.z = 200
>>> 
Traceback (most recent call last):
  File "solt_demo.py", line 64, in <module>
    a.z = 200
AttributeError: 'A' object has no attribute 'z'

实例的__dict__属性不存在

print(a.__dict__)
Traceback (most recent call last):
  File "solt_demo.py", line 64, in <module>
    print(a.__dict__)
AttributeError: 'A' object has no attribute '__dict__'

类属性不受影响

class A():
    __slots__=('x', "y") 
    def __init__(self,x,y):
        self.x = x
        self.y = y 

A.new = 300
print(A.new)

子类不具有__slots__属性

父类中有__slots__,子类继承父类时子类不具有__slots__属性,可以操作实例的属性。

class A():
    __slots__=('x', "y") 
    def __init__(self,x,y):
        self.x = x
        self.y = y 


class B(A): 
    pass 

b = B(33,44)
print(b.x)
print(b.y)


b.z = 55
print(b.z)
print(b.__dict__)

更节省内存

不使用__slots__

from  memory_profiler import profile

class A():
    def __init__(self,x,y):
        self.x = x
        self.y = y 


@profile
def main():
    object_list = [A(100,20) for i in range(100000)]

if __name__=='__main__':
    main()
(python3.8) ➜  sublime python -m memory_profiler slots_demo.py
Filename: slots_demo.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     9     13.8 MiB     13.8 MiB           1   @profile
    10                                         def main():
    11     30.0 MiB     16.2 MiB      100003       object_list = [A(100,20) for i in range(100000)]

使用__slots__

from  memory_profiler import profile

class A():
    __slots__ = ("x", "y")
    def __init__(self,x,y):
        self.x = x
        self.y = y 


@profile
def main():
    object_list = [A(100,20) for i in range(100000)]

if __name__=='__main__':
    main()
(python3.8) ➜  sublime python -m memory_profiler slots_demo.py
Filename: slots_demo.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    10     13.4 MiB     13.4 MiB           1   @profile
    11                                         def main():
    12     18.9 MiB      4.1 MiB      100003       object_list = [A(100,20) for i in range(100000)]

从结果来看不定义__slots__增加了16.2MB,定义__slots__增加了4.1MB,所以有理由相信有__slots__节省四分之三的内存。
节省内存的原因:
普通类使用字典保存所有属性,定义了__slots__之后使用列表保存所有属性,减少了内存的使用(因为属性个数固定,所有可以使用有序数据的列表,列表相比字典减少了内存消耗)
注意:
但是节省只在创建大量实例时才能体现。

总结

定义__slots__属性之后的特点如下:

  1. 实例的__dict__属性不存在,节省一定内存
  2. 不可以给实例动态绑定属性,但类的属性不受影响
  3. 子类继承有__solts__的父类时,不拥有__solts__,也就是子类不受限制
  4. 节省内存

参考:
https://blog.csdn.net/sxingming/article/details/52892640

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章