我想在某些數據不適合內存的情況下使用memmap,並且使用memmap將代碼欺騙的想法僅僅是一個ndarray。爲了進一步擴展這種使用memmap的方式,我想知道是否有可能重載memmap的取消引用操作符來刪除memmap文件。在Python中,當memmap對象不再被引用時,是否有可能重載Numpy的memmap來刪除它自己?
因此,例如:
from tempfile import mkdtemp
import os.path as path
filename = path.join(mkdtemp(), 'tmpfile.dat')
{
out = np.memmap(filename, dtype=a.dtype, mode='w+', shape=a.shape)
}
# At this point out is out of scope, so the overloaded
# dereference function would delete tmpfile.dat
這聽起來是可行的/有這個已經完成?有沒有我沒有想到的東西?
謝謝!
「引用操作」? –
'{''}'?! 'from __future__ import braces' ... – glglgl
你確定不想使用'with'上下文管理器嗎? 'with'是Python如何進行基於範圍的資源管理;這與C++的RAII最爲接近。 – user2357112