Python:numpy.memmap零、重用、文档?

Python:numpy.memmap零、重用、文档?,python,numpy,Python,Numpy,numpy.memmap数组是否初始化为零?这在哪里有记录?是否可以将已存在的旧的numpy.memmap文件(来自以前执行的脚本)的内容加载到新的numpy.memmap中,而不是替换为零?谢谢 memmapdoctest示例似乎非常清楚这一点: Create a memmap with dtype and shape that matches our data: >>> fp = np.memmap(filename, dtype='float32', mode='w+'

numpy.memmap数组是否初始化为零?这在哪里有记录?是否可以将已存在的旧的numpy.memmap文件(来自以前执行的脚本)的内容加载到新的numpy.memmap中,而不是替换为零?谢谢

memmap
doctest
示例似乎非常清楚这一点:

Create a memmap with dtype and shape that matches our data:

>>> fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4))
>>> fp
memmap([[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]], dtype=float32)
新创建的文件中的数据均为零

在写完并结束之后

Load the memmap and verify data was stored:

>>> newfp = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))
>>> newfp
memmap([[  0.,   1.,   2.,   3.],
        [  4.,   5.,   6.,   7.],
        [  8.,   9.,  10.,  11.]], dtype=float32)
现有文件中的数据是上次使用时遗留下来的


在源头

def\uuuu new\uuuuuu(子类型,文件名,dtype=uint8,mode='r+',offset=0,
shape=None,order='C'):
...
如果模式=='w+'或(模式=='r+'和flen<字节):
fid.seek(字节-1,0)
fid.write(np.compat.asbytes('\0'))
fid.flush()

如果我读的是正确的,它就是在文件的开头(或现有文件的结尾)写一个0<代码>mmap.mmap可能决定对所需长度的其余部分执行什么操作。

您尝试过其中任何一种吗?发生了什么事?在笔记部分有零的行为。谢谢@wwii!请参阅我在接受答案下的完整评论。示例文档可能会令人困惑和不完整。我将在这里明确说明答案:
mode='w+'
初始化为零;所有其他模式值都使用旧的memmap文件内容初始化。谢谢你@hpaulj和@wwii
 def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0,
            shape=None, order='C'):
    ...
    if mode == 'w+' or (mode == 'r+' and flen < bytes):
        fid.seek(bytes - 1, 0)
        fid.write(np.compat.asbytes('\0'))
        fid.flush()