why won't slicing lists raise IndexError?

Ned Batchelder ned at nedbatchelder.com
Mon Dec 4 21:50:13 EST 2017


On 12/4/17 9:31 PM, Rick Johnson wrote:
> On Monday, December 4, 2017 at 7:47:20 PM UTC-6, Ned Batchelder wrote:
>
> [...]
>      
>> Here are details filled in:
>>
>>      $ python3.6
>>      Python 3.6.3 (default, Oct  4 2017, 06:03:25)
>>      [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
>>      Type "help", "copyright", "credits" or "license" for more information.
>>       >>> def do_the_thing(seq, n):
>>      ...     item = seq[n:n+1]
>>      ...     if item:
>>      ...         print(f"process({item})")
>>      ...     else:
>>      ...         print("do_without_item()")
>>      ...
>>       >>> do_the_thing([1, 2, 3], 2)
>>      process([3])
>>       >>> do_the_thing([1, 2, 3], 5)
>>      do_without_item()
>>       >>>
> Thanks for filling in the blanks. However, my objection to
> this else-clause stems from a perspective based in
> pragmatics. Specifically, i see no benefit here in logging
> the "non-action". Sure, perhaps logging a non-action may serve a
> useful purpose during debugging sessions, but i find them to
> be nothing but useless noise in production code.
>
> Do you agree or disagree with my assessment?
>
> If you disagree, please explain why.

The point of the example was to demonstrate what happens when slicing 
beyond the bounds of the list.  It's beyond the scope of the thread to 
debate whether you might want to perform an action in that case.  I 
think we've demonstrated the slicing semantics well.

--Ned.



More information about the Python-list mailing list