why won't slicing lists raise IndexError?

Rick Johnson rantingrickjohnson at gmail.com
Mon Dec 4 17:22:54 EST 2017


Terry Reedy wrote:

[...]
    
> try:
>      item = seq[n]
> except IndexError
>      do_without_item()
> else:
>      process(item)
> 
> item = seq[n:n+1]
> if item:
>      process(item)
> else:
>      do_without_item()
> 
> Many prefer the second.  

And they'll prefer it even more when they realize the entire ELSE
clause of your latter example is superfluous.

But there are many usages where the conditional logic is not
needed at all. Sometimes you don't care what you get back
from a slice. Yep, even when the slice is a null string.



More information about the Python-list mailing list