Python是一門動態語言,可以在運行過程中,修改實例的屬性和增刪方法。任何實例都包含一個字典__dict__
,該字典保存了實例所有的屬性和方法。Python也通過這個字典可以將任意屬性綁定到實例上。
有時候在實例的生命週期中處於安全等考慮只能操作固定的屬性,不想增加屬性,可以通過__slots__
來就可以定義一個集合,只有在這個集合中的屬性才能被操作。
__slots__
是一個類的屬性,有三個功能:
- 實例不能訪問或添加__slots__之外的屬性
- 實例沒有__dict__方法
- 節省內存,實例保存屬性的結構從字典變成列表
不使用__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
特點:
- 可以通過
a.__dict__
輸出實例所有屬性 - 類變量不在
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__屬性之後的特點如下:
- 實例的__dict__屬性不存在,節省一定內存
- 不可以給實例動態綁定屬性,但類的屬性不受影響
- 子類繼承有__solts__的父類時,不擁有__solts__,也就是子類不受限制
- 節省內存
參考:
https://blog.csdn.net/sxingming/article/details/52892640