[Numpy-discussion] Regression: in-place operations (possibly intentional)

Nathaniel Smith njs@pobox....
Thu Sep 20 16:48:26 CDT 2012


On Wed, Sep 19, 2012 at 1:08 AM, Charles R Harris
<charlesr.harris@gmail.com> wrote:
> <snip>
>
> The relevant setting is in numpy/core/include/numpy/ndarraytypes.h
>
> #define NPY_DEFAULT_ASSIGN_CASTING NPY_SAME_KIND_CASTING
>
> I think that if we want to raise a warning we could define a new rule,
>
> NPY_WARN_SAME_KIND_CASTING
>
> Which would do the same as unsafe, only raise a warning on the way.

https://github.com/numpy/numpy/pull/451

Query: I would have thought that NPY_DEFAULT_ASSIGN_CASTING would
determine the default casting used for assignments. But in current
master:

>>> a = np.zeros(3, dtype=int)
>>> a[0] = 1.1
>>> a
array([1, 0, 0])

In fact, this variable seems to only be used by PyArray_Std,
PyArray_Round, and ufuncs. Okay, so, NPY_DEFAULT_ASSIGN_CASTING is
just misnamed, but -- what casting rule *should* plain old assignment
follow? I'd think same_kind casting is probably a good default here
for the same reason it's a good default for ufuncs, and because a += b
really should be the same as a = a + b. But, the only problem is, how
could you override it if desired? a.__setitem__(0, casting="unsafe")?

-n


More information about the NumPy-Discussion mailing list