26 Nov
2020
26 Nov
'20
8:31 a.m.
Hello,
On Thu, 26 Nov 2020 13:27:46 +1300
Greg Ewing
On 26/11/20 12:41 pm, Steven D'Aprano wrote:
a = "abcdef" a[-2] # returns a result
Yes, but did you *intend* that result, or did the -2 result from a calculation that should have returned a positive index but went wrong? Python has no way to tell.
Certainly it does: idx = index_calc() assert idx >= 0 Compare that with the case where index of 5 is invalid (just can't happen per algorithm invariants). Python (or any other language) "has no way to tell" that just from looks of your algorithm, and yet it's easy to tell that: idx = index_calc() assert idx != 5 -- Best regards, Paul mailto:pmiscml@gmail.com