Does assignment with advanced indexing copy array data?

后端 未结 3 480
南笙
南笙 2020-12-09 05:11

I am slowly trying to understand the difference between views and copys in numpy, as well as mutable vs. immutable types.

If I access part

3条回答
  •  醉酒成梦
    2020-12-09 05:20

    When you do c = a[b], a.__get_item__ is called with b as its only argument, and whatever gets returned is assigned to c.

    When you doa[b] = c, a.__setitem__ is called with b and c as arguments and whatever gets returned is silently discarded.

    So despite having the same a[b] syntax, both expressions are doing different things. You could subclass ndarray, overload this two functions, and have them behave differently. As is by default in numpy, the former returns a copy (if b is an array) but the latter modifies a in place.

提交回复
热议问题