Hacker News new | ask | show | jobs
by amelius 1989 days ago
Do you really think so? I'd personally feel more reassured if a[-1] caused an assertion to fail.

Python should have used a different notation, imho. Like: a[<0] for the last element.

1 comments

I don't know; the grammar is already pretty complicated without introducing additional indexing schemes. Now that you mention it though, I don't know that I've ever written an algorithm mixing and matching negative/positive indices in a way that couldn't be trivially re-written with that kind of syntax. I'm sure such cases exist for somebody...

Looking for solutions, if you're stuck with Python and hate that behavior:

- If you don't need errors raised then a[~i] is already equal to a[-i-1].

- In your own code (or at its boundaries when wrapping external lists) it's nearly free and not much code to subclass list, override __getitem__, and raise errors for negative indices while responding to some syntax like a[0,] or a.R[0].