[Python-ideas] PEP pre-draft: Support for indexing with keyword arguments
Stefano Borini
stefano.borini at ferrara.linux.it
Thu Jul 3 21:33:56 CEST 2014
On Thu, Jul 03, 2014 at 10:57:48AM -0700, Stephan Hoyer wrote:
> don't have strong opinions about the implementation, but I am strongly
> supportive of this PEP for the second case it lists -- the ability to index
> an a multi-dimensional array by axis name or label instead of position.
thinking aloud.
The biggest problem is that there's no way of specifying which labels the
object supports, and therefore no way of binding a specified keyword, unless
the __getitem__ signature is deeply altered.
> It is awkward and error prone to use the existing __getitem__ and
> __setitem__ syntax, because it's difficult to reliably keep track of axis
> order with this many indices:
>
> a[:, :, 0:10]
>
> vs.
>
> a[y=0:10]
This is indeed an important use case. I should probably stress it more in the
PEP.
> Another issue: The PEP should address whether expressions with slice
> abbreviations like the following should be valid syntax:
>
> a[x=:, y=:5, z=::-1]
looks ugly indeed
> Surrounding the indices with [] might help:
>
> a[x=[:], y=[:5], z=[::-1]]
better, but unusual
> -1 from me. The existing get method handles this case pretty well, with
> fewer keystrokes than the keyword only "default" index (as I think has
> already been pointed out).
>
> In my opinion, case 1 (labeled indices for a physics DSL) and case 2
> (labeled indices to removed ambiguity) are basically the same, and the only
> use-cases that should be encouraged. Labeling tensor indices with names in
> mathematical notation is standard for precisely the same reasons that it's
> a good idea for Python.
Meaning dropping the use of keyword indexing for "options" use cases.
More information about the Python-ideas
mailing list