
On Thu, Dec 29, 2022 at 8:50 AM Diogo Valada <diogovalada.7@hotmail.com> wrote:
Hi all,
New to the mailing list, so I hope I'm creating a discussion in the right place.
Am I the only one that thinks that Advanced indexing in numpy doesn't follow the principle of minimum astonishment?
for example
```python a = np.random.rand(100, 100)
a[(2,4)] #this yields the element at [2,4] a[[2,4]] #this yields the rows at position 2 and 4 a[1, (2,4)] #this yields the 2nd and 4th elements of row 1. (So actually does advanced indexing) a[1, [2,4]] # Works the same way as the previous one. ```
Worst of all, it's very easy for someone do a mistake and not notice it: it seems to me that the first method, a[(2,4)], should not be allowed, and instead only a[*(2,4)] should work. How checked how it works in Julia (which has a similar syntax), and a[(2,4)] would yield an error, which makes sense to me. Could it be an idea to deprecate a[(2,4)]-like usages?
No, that's not possible. In Python syntax, the comma `,` is what creates the tuple, not the parentheses. So `a[(2,4)]` is exactly `a[2, 4]`. `a[2, 4]` translates to `a.__getitem__((2, 4))`. So there's no way for the array to know whether it got `a[2, 4]` or `a[(2, 4)]`. -- Robert Kern