Open craffel opened 5 years ago
For now to avoid silent surprises we should probably override __iadd__
and its ilk to raise an error, since otherwise Python will desugar b += 10
to b = b + 10
and thus behave differently from NumPy. We could add in-place operations at some point, probably with different semantics than NumPy's, but for now we should probably be explicit.
Any updates on this? I think this is a big pitfall behaviour for new users. Matching numpy's read-only behaviour here would be great:
import numpy as np
x = np.arange(5)
x.flags.writeable = False
x += 1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-42-4068e55d63c2> in <module>()
3 x = np.arange(5)
4 x.flags.writeable = False
----> 5 x += 1
ValueError: output array is read-only
Simple example:
This difference is counterintuitive if you're used to numpy's referencing behavior. This came up when looping over lists of
jax.numpy.ndarray
s:A workaround suggested by @mattjj which is arguably at least as Pythonic: