On Dec 12, 2019, at 09:09, Jonathan Fine <[email protected]> wrote:
>
> Finally, off-topic, I find it a bit odd that in the below we get syntax
> errors (rather than a run-time error).
>
> >>> getslice[]
> SyntaxError: invalid syntax
> >>> getslice[1:2]
> slice(1, 2, None)
> >>> getslice[1:2:3]
> slice(1, 2, 3)
> >>> getslice[1:2:3:4]
> SyntaxError: invalid syntax
What would you expect these to return?
For the empty one, you obviously can’t distinguish between no colons separating
nothing and any other empty space. (In fact, notice that this is similar to the
way tuples of 1 element require the trailing comma and tuples of 0 elements
require the parentheses, because otherwise everything and even nothing would be
ambiguous as a tuple display, and you couldn’t parse anything until you’d
leaned how get no tea and tea in your inventory at the same time.)
For the fourth one, slices aren’t just arbitrary sequences of numbers, they’re
a start, stop, and step value (and at least the stop has to be specified). It
shouldn’t be surprising that the constructor signature looks very similar to
the one for range. And the syntax just follows that constructor signature. What
would the start, stop, and step be for 1:2:3:4, and where would the extra value
be stored?
If you do want to index with arbitrary sequences of numbers, just abuse tuples
instead of slices:
getaxes = getslice
getaxes[1, 2, 3, 4]
If you already need to use tuples for dimensions and also need something to
abuse for some other purpose, then I guess you’re out of luck and will have to
write your own thing that takes a few characters to spell instead of one:
class T(list): pass
getaxes[T(1,2,3,4)]
But I think at that point you’re probably better off explicitly using index
arrays and bool arrays as your indexes, as numpy does.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/IABOB4BZAITWIOEK7T3L5L5BXFP4WUO4/
Code of Conduct: http://python.org/psf/codeofconduct/