Python中__slots__屬性介紹與基本使用方法
簡介
在廖雪峰的python網(wǎng)站上,他是這么說的
python是動態(tài)語言,它允許程序在執(zhí)行過程中動態(tài)綁定屬性或者方法(使用MethodTpye)。
某個實例在執(zhí)行過程中綁定的屬性跟方法,僅在該實例中有效,其他同類實例是沒有的。
可以通過給class綁定屬性/方法,來給所有實例綁定屬性/方法:
Student.name = '' Student.set_score = set_score
而如果使用__slots__,它僅允許動態(tài)綁定()里面有的屬性
例如,下面這樣會報錯
class Student(): __slots__ = ('name', 'age') S1 = Student() S1.name = 'Jack' # ok! S1.score = 123 # error!
但是我覺得很奇怪,僅有這一個作用嗎?于是我再查了其他資料,發(fā)現(xiàn)這個函數(shù)可以很可觀地節(jié)約內存,下面來一起看看詳細的介紹吧。
__slots__允許我們聲明并限定類成員,并拒絕類創(chuàng)建__dict__和__weakref__屬性以節(jié)約內存空間。
Python是動態(tài)語言,對于普通的類,可以為類實例賦值任何屬性,這些屬性會存儲在__dict__中:
>>> class Student(object): ... pass ... >>> Abey = Student() >>> Abey.name = 'Abey' >>> Abey.__dict__ {'name': 'Abey'}
這樣的特性帶來兩個問題:
- 數(shù)據(jù)通過字典(Hash)存儲所占用的空間較大
- 如何禁止隨意生成類屬性
當然,__slots__就能解決這兩個問題。通過__slots__屬性限定類屬性的創(chuàng)建:
>>> class Student(object): ... __slots__ = ('name', 'age') ... >>> Abey = Student() >>> Abey.name = 'Abey' >>> Abey.gender = 'Female' Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'Student' object has no attribute 'gender' >>> Abey.__dict__ Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'Student' object has no attribute '__dict__'
可以看到,在定義了__slots__變量后,Student類實例已經(jīng)不能隨意創(chuàng)建不在__slots__定義內的屬性gender,同時實例中也不再有__dict__結構。
用法
繼承樹
__slots__在繼承中有兩種表現(xiàn):
- 子類未聲明__slots__時,不繼承父類的__slots__,即此時子類實例可以隨意賦值屬性
- 子類聲明__slots__時,繼承父類的__slots__,即此時子類的__slots__為其自身+父類的__slots__
以下面的父類為例:
>>> class Student(object): ... __slots__ = ('name', 'age') ...
創(chuàng)建一個子類不聲明__slots__,該類實例可以創(chuàng)建父類__slots__限定之外的屬性gender:
>>> class SubStudent(Student): ... pass ... >>> Bob = SubStudent() >>> Bob.gender = 'Male' >>> Bob.__dict__ {'gender': 'Male'}
而創(chuàng)建一個聲明__slots__的子類,該類屬性則只能創(chuàng)建父類__slots__+自身__slots__限定的屬性:
>>> class SubStudent2(Student): ... __slots__ = 'gender' ... >>> Cathy = SubStudent2() >>> Cathy.gender = 'Female' >>> Cathy.name = 'Cathy' >>> Cathy.teacher = 'Mrs. Wang' Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'SubStudent2' object has no attribute 'teacher'
注意:子類的__slots__本身已經(jīng)繼承自父類,無需重復聲明父類已聲明的屬性。例如上例,重復聲明會多占用內存空間:
>>> class SubStudent3(Student): ... __slots__ = ('name', 'age', 'gender') ... >>> from sys import getsizeof >>> getsizeof(Student()), getsizeof(SubStudent2()), getsizeof(SubStudent3()) (56, 64, 80)
性能對比
我們?yōu)槭裁匆褂胈_slots__呢?
更快速地賦值屬性
參考Stack Overflow回答中給出的數(shù)據(jù):
import timeit class Foo(object): __slots__ = 'foo', class Bar(object): pass slotted = Foo() not_slotted = Bar() def get_set_delete_fn(obj): def get_set_delete(): obj.foo = 'foo' obj.foo del obj.foo return get_set_delete
得到測試結果為:
>>> min(timeit.repeat(get_set_delete_fn(slotted)))
0.2846834529991611
>>> min(timeit.repeat(get_set_delete_fn(not_slotted)))
0.3664822799983085
可以看到,在相同的環(huán)境(Ubuntu)下,slots為Python3.5帶來了接近30%的賦值速度提升:
節(jié)約內存空間
>>> 0.3664822799983085 / 0.2846834529991611 1.2873325658284342
由于不使用__dict__存儲對象的屬性,__slots__在一些場景下能夠節(jié)約極大的內存空間。具體數(shù)據(jù)可以查看參考中的回答鏈接,不贅述。
參考
[1] Usage of __slots__? , Aaron Hall, Stack Overflow
推薦閱讀
[1] Data model , Python Document
[2] python __slots__ 使你的代碼更加節(jié)省內存 , david_bj, 51CTO
[3] 使用__slots__ , 廖雪峰
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
python神經(jīng)網(wǎng)絡Keras實現(xiàn)GRU及其參數(shù)量
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡Keras實現(xiàn)GRU及其參數(shù)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05如何使用python數(shù)據(jù)處理解決數(shù)據(jù)沖突和樣本的選取
這篇文章主要介紹了如何使用python數(shù)據(jù)處理解決數(shù)據(jù)沖突和樣本的選取,其中主要包括 實際業(yè)務數(shù)據(jù)沖突、樣本選取問題、數(shù)據(jù)共線性等思路2021-08-08