1. 程式人生 > 其它 >Python中減少記憶體消耗的方法之一:__slots__屬性

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