On 11/25/2020 6:41 PM, Steven D'Aprano wrote:
On Thu, Nov 26, 2020 at 12:07:56PM +1300, Greg Ewing wrote:
On 26/11/20 2:30 am, nathan.w.edwards@outlook.com wrote:
At times I heavily rely on index out of bound exceptions to reduce the number of lines necessary for error checking. This is a downside to the negative indexing scheme -- you can't tell the difference between a backwards index and an error due to out-of-bounds indexing. Sorry, perhaps I am being a bit dim-witted this morning, but I don't understand this. Surely the difference is obvious?
a = "abcdef" a[-2] # returns a result a[999] # raises an exception
Obviously you can tell the two apart, so I'm confused by your comment.
If they're literals, it's easy. But a[an_index-an_offset] isn't so easy to spot, say if you're using the wrong offset for example. In other programming languages, it might be an error for the index to be negative. In Python it's not, if the result <= -length(a). Sure, you could use an assert, but many people won't, and the assert can be optimized away. I've been burned by this a number of times. Eric