python?魔法方法之?__?slots?__的實(shí)現(xiàn)
__ slots __
__slots__是python class的一個(gè)特殊attribute,能夠節(jié)省內(nèi)存空間。正常情況下,一個(gè)類的屬性是以字典的形式來管理, 每個(gè)類都會(huì)有__ dict__ 方法。但是我們可以通過 設(shè)置 __ slots__ 來將類的屬性構(gòu)造成一個(gè)靜態(tài)的數(shù)據(jù)結(jié)構(gòu)來管理,里面存儲(chǔ)的是 value references。
class Bar(object):
def __init__(self, a):
self.a = a
class BarSlotted(object):
__slots__ = "a",
def __init__(self, a):
self.a = a
# create class instance
bar = Bar(1)
bar_slotted = BarSlotted(1)
print(set(dir(bar)) - set(dir(bar_slotted)))
# {'__dict__', '__weakref__'}
'''
使用 __slots__ 后, 類里面會(huì)減少 __dict__ __weakref__ 兩個(gè)方法。
__weakref__ --- 弱引用 詳情鏈接 https://docs.python.org/zh-cn/3/library/weakref.html
'''
優(yōu)點(diǎn):
- 節(jié)約內(nèi)存,不用去定義動(dòng)態(tài)數(shù)據(jù)接口
__ dict__的內(nèi)存,__ weakref__也不用去申請(qǐng) - access attributes 更快,靜態(tài)數(shù)據(jù)結(jié)構(gòu),比
__ dict__更快
缺點(diǎn):
定義死后,不能去申請(qǐng)新的屬性,申請(qǐng)會(huì)報(bào)屬性錯(cuò)誤

可以通過 把 __ dict__ 作為 __ slots__ 的一個(gè)屬性,實(shí)現(xiàn)既能通過定義__ slots__ 節(jié)約內(nèi)存,又實(shí)現(xiàn)新屬性的定義。
class BarSlotted(object):
__slots__ = "a",'__dict__'
def __init__(self, a):
self.a = a
bar_slotted = BarSlotted(1)
bar_slotted.b = "111"
當(dāng)你事先知道class的attributes的時(shí)候,建議使用slots來節(jié)省memory以及獲得更快的attribute access。
注意不應(yīng)當(dāng)用來限制__slots__之外的新屬性作為使用__slots__的原因,可以使用裝飾器以及反射的方式來實(shí)現(xiàn)屬性控制。
注意事項(xiàng)
子類會(huì)繼承父類的 __ slots__
class Parent(object):
__slots__ = "x", "y"
class Child(Parent):
__slots__ = "z",
# 重復(fù)的 x, y 可以不寫,
# __slots__ = "x", "y", "z"
child = Child()
print(dir(child))
# [..., 'x', 'y', 'z']
不支持多繼承, 會(huì)直接報(bào)錯(cuò)
class ParentA(object):
__slots__ = "x",
class ParentB(object):
__slots__ = "y",
class Child(ParentA, ParentB):
pass
'''
Traceback (most recent call last):
File "C:/Users/15284/PycharmProjects/pythonProject/test.py", line 69, in <module>
class Child(ParentA, ParentB):
TypeError: multiple bases have instance lay-out conflict
'''
只允許父類中有一方設(shè)定了 __ slots__
class AbstractA(object): __slots__ = () class AbstractB(object): __slots__ = "x" class Child(AbstractA, AbstractB): __slots__ = "x", "y"
新版本的 pickle中的 pickle含有slotted class,使用時(shí)需要注意
到此這篇關(guān)于python 魔法方法之 __ slots __的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python __ slots __內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中__slots__節(jié)約內(nèi)存的具體做法
- Python __slots__的使用方法
- 通過實(shí)例了解python__slots__使用方法
- python3中使用__slots__限定實(shí)例屬性操作分析
- Python類中的魔法方法之 __slots__原理解析
- python使用__slots__讓你的代碼更加節(jié)省內(nèi)存
- Python中__slots__屬性介紹與基本使用方法
- Python中的__slots__示例詳解
- python中__slots__用法實(shí)例
- 在Python中使用__slots__方法的詳細(xì)教程
- 用Python中的__slots__緩存資源以節(jié)省內(nèi)存開銷的方法
- python中的__slots__使用示例
相關(guān)文章
python用10行代碼實(shí)現(xiàn)對(duì)黃色圖片的檢測(cè)功能
這篇文章主要介紹了python用10行代碼實(shí)現(xiàn)對(duì)黃色圖片的檢測(cè)功能,涉及Python基于圖片庫(kù)PIL對(duì)圖片的檢測(cè)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
python sort、sort_index方法代碼實(shí)例
這篇文章主要介紹了python sort、sort_index方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
OpenCV-Python使用分水嶺算法實(shí)現(xiàn)圖像的分割與提取
在圖像的處理過程中,經(jīng)常需要從圖像中將前景對(duì)象作為目標(biāo)圖像分割或者提取出來。本文就介紹了使用分水嶺算法實(shí)現(xiàn)圖像的分割與提取,感興趣的可以了解一下2021-06-06
Python實(shí)現(xiàn)多張圖片合成文字的效果
前段時(shí)間看到有人問如何使用Python實(shí)現(xiàn)多張圖片組成文字的效果?覺得還挺有意思,于是嘗試做了一下,剛好趕上端午節(jié),所以打算從網(wǎng)上下載1000張王心凌的照片,組成端午安康的字樣,感興趣的可以了解一下2022-06-06
python基礎(chǔ)之包的導(dǎo)入和__init__.py的介紹
這篇文章主要介紹了python基礎(chǔ)之包的導(dǎo)入和__init__.py的相關(guān)資料,需要的朋友可以參考下2018-01-01

