不可变numpy的阵列? [英] Immutable numpy array?
问题描述
有一个简单的方法来创建一个不可变的numpy的阵列?
Is there a simple way to create an immutable NumPy array?
如果一个人得出从 ndarray
A类来做到这一点,什么是最低限度的方法,一个具有覆盖达到不变性?
If one has to derive a class from ndarray
to do this, what's the minimum set of methods that one has to override to achieve immutability?
推荐答案
您可以做一个numpy的阵列不可写:
You can make a numpy array unwriteable:
a = np.arange(10)
a.flags.writeable = False
a[0] = 1
# Gives: RuntimeError: array is not writeable
也看到了这一主题的讨论:
Also see the discussion in this thread:
<一个href=\"http://mail.scipy.org/pipermail/numpy-discussion/2008-December/039274.html\">http://mail.scipy.org/pipermail/numpy-discussion/2008-December/039274.html
和文档:
<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flags.html\">http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flags.html
这篇关于不可变numpy的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!