# __slots__魔法
# `__slots__`魔法
在Python中,每個類都有實例屬性。默認情況下Python用一個字典來保存一個對象的實例屬性。這非常有用,因為它允許我們在運行時去設置任意的新屬性。
然而,對于有著已知屬性的小類來說,它可能是個瓶頸。這個字典浪費了很多內存。Python不能在對象創建時直接分配一個固定量的內存來保存所有的屬性。因此如果你創建許多對象(我指的是成千上萬個),它會消耗掉很多內存。
不過還是有一個方法來規避這個問題。這個方法需要使用`__slots__`來告訴Python不要使用字典,而且只給一個固定集合的屬性分配空間。
這里是一個使用與不使用`__slots__`的例子:
-
不使用 `__slots__`:
~~~
class MyClass(object):
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
self.set_up()
# ...
~~~
-
使用 `__slots__`:
~~~
class MyClass(object):
__slots__ = ['name', 'identifier']
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
self.set_up()
# ...
~~~
第二段代碼會為你的內存減輕負擔。通過這個技巧,有些人已經看到內存占用率幾乎40%~50%的減少。
稍微備注一下,你也許需要試一下PyPy。它已經默認地做了所有這些優化。
以下你可以看到一個例子,它用IPython來展示在有與沒有`__slots__`情況下的精確內存占用,感謝 [https://github.com/ianozsvald/ipython_memory_usage](https://github.com/ianozsvald/ipython_memory_usage)
~~~
Python 3.4.3 (default, Jun 6 2015, 13:32:34)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import ipython_memory_usage.ipython_memory_usage as imu
In [2]: imu.start_watching_memory()
In [2] used 0.0000 MiB RAM in 5.31s, peaked 0.00 MiB above current, total RAM usage 15.57 MiB
In [3]: %cat slots.py
class MyClass(object):
__slots__ = ['name', 'identifier']
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
num = 1024*256
x = [MyClass(1,1) for i in range(num)]
In [3] used 0.2305 MiB RAM in 0.12s, peaked 0.00 MiB above current, total RAM usage 15.80 MiB
In [4]: from slots import *
In [4] used 9.3008 MiB RAM in 0.72s, peaked 0.00 MiB above current, total RAM usage 25.10 MiB
In [5]: %cat noslots.py
class MyClass(object):
def __init__(self, name, identifier):
self.name = name
self.identifier = identifier
num = 1024*256
x = [MyClass(1,1) for i in range(num)]
In [5] used 0.1758 MiB RAM in 0.12s, peaked 0.00 MiB above current, total RAM usage 25.28 MiB
In [6]: from noslots import *
In [6] used 22.6680 MiB RAM in 0.80s, peaked 0.00 MiB above current, total RAM usage 47.95 MiB
~~~
- 簡介
- 序
- 譯后感
- 原作者前言
- *args 和 **kwargs
- *args 的用法
- **kwargs 的用法
- 使用 *args 和 **kwargs 來調用函數
- 啥時候使用它們
- 調試 Debugging
- 生成器 Generators
- 可迭代對象(Iterable)
- 迭代器(Iterator)
- 迭代(Iteration)
- 生成器(Generators)
- Map和Filter
- Map
- Filter
- set 數據結構
- 三元運算符
- 裝飾器
- 一切皆對象
- 在函數中定義函數
- 從函數中返回函數
- 將函數作為參數傳給另一個函數
- 你的第一個裝飾器
- 使用場景
- 授權
- 日志
- 帶參數的裝飾器
- 在函數中嵌入裝飾器
- 裝飾器類
- Global和Return
- 多個return值
- 對象變動 Mutation
- slots魔法
- 虛擬環境
- 容器 Collections
- 枚舉 Enumerate
- 對象自省
- dir
- type和id
- inspect模塊
- 推導式 Comprehension
- 列表推導式
- 字典推導式
- 集合推導式
- 異常
- 處理多個異常
- finally從句
- try/else從句
- lambda表達式
- 一行式
- For - Else
- else語句
- open函數
- 目標Python2+3
- 協程
- 函數緩存
- Python 3.2+
- Python 2+
- 上下文管理器
- 基于類的實現
- 處理異常
- 基于生成器的實現