You are looking at the documentation for Numpy 1.15, and this uses a new feature of nditer()
introduced in that release:
Under certain conditions, nditer must be used in a context manager
When using an numpy.nditer
with the "writeonly"
or "readwrite"
flags, there are some circumstances where nditer
doesn’t actually give you a view of the writable array. Instead, it gives you a copy, and if you make changes to the copy, nditer
later writes those changes back into your actual array. Currently, this writeback occurs when the array objects are garbage collected, which makes this API error-prone on CPython and entirely broken on PyPy. Therefore, nditer
should now be used as a context manager whenever it is used with writeable arrays, e.g., with np.nditer(...) as it: ...
. You may also explicitly call it.close()
for cases where a context manager is unusable, for instance in generator expressions.
The error indicates you have an earlier version of Numpy; the with
statement only works with context managers, which must implement __exit__
(and __enter__
), and the AttributeError
exception indicates that in your Numpy version the required implementation isn’t there.
Either upgrade, or don't use with
:
for x in np.nditer(a, op_flags=['readwrite']):
x[...] = 2 * x
When using CPython you may still run into the issues that caused the change made in the 1.15 release however. When using PyPy you will run into those issues, and upgrading is your only proper recourse.
You probably want to refer to the 1.14 version of the same documentation entry you used (or more specifically, make sure to pick the right documentation for your local version.