why won't slicing lists raise IndexError?

Terry Reedy tjreedy at udel.edu
Wed Dec 6 16:43:35 EST 2017


On 12/5/2017 9:23 PM, Rick Johnson wrote:
> Steve D'Aprano wrote:
> 
> [...]
> 
>> You've already been told that there's no indication or
>> reason to believe that it is a non-action. You've already
>> been given at least one possible action. It isn't a non-
>> action, it is two distinct actions:
>>
>> - the action you take when the slice is non-empty;
>>
>> - the action you take when the slice is empty.
> 
> When Python follows a logic clause like a train skating
> along a set of railroad tracks, and finds itself in a *GHOST
> TOWN*, that's not an action -- "Steve-o" -- it's a non-
> action.

Rick, cut the crap.  If you do not understand that 'something_else()' != 
'pass', re-read the tutorial.
---

The OP asked: "Why is slicing 'forgiving'?"  The current behavior could 
be interpreted as 'letting errors pass silently'.  It would be if 
slicing meant 'give me exactly the length stop-start subsequence from 
start to stop (or raise)'.  But slicing actually means 'give me whatever 
subsequence exists between start and stop'.

My examples attempted to show why this looser definition of slicing is 
*useful*.  The focus of the third example was entirely on the condition, 
not the alternative actions.  (Reminder: either action can be the 
if-action, and the other the else-action, depending on how the condition 
is written.)

I should have mentioned, and others did, that the OP can write a custom 
__getitem__ method that implements stricter slicing for instances of a 
custom class.

I think we have collectively answered the OP's question quite well.

-- 
Terry Jan Reedy




More information about the Python-list mailing list