why won't slicing lists raise IndexError?

Ned Batchelder ned at nedbatchelder.com
Mon Dec 4 20:47:06 EST 2017


On 12/4/17 8:03 PM, Rick Johnson wrote:
> On Monday, December 4, 2017 at 6:13:19 PM UTC-6, Chris Angelico wrote:
>
> [...]
>      
>> Ahhh, I see how it is. You didn't run the code, ergo you
>> don't understand it. Makes perfect sense. :)
> Being that Terry didn't offer any declarations or defintions
> for his variables or functions, i assumed, i suppose, like
> any other rational person, that it was all just pseudo code.
>
> But, in any event, i don't see how running the code and then
> watching helplessly as a volley of NameErrors come rip-
> roaring down a stdout stream like a half naked, middle-aged
> drunk feller bobbing precariously along a river rapid --
> hey, look out for that rock! -- is going to help shed any
> light on any of this.
>
> But i suppose it's possible i missed an esoteric joke
> somewhere...

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()
     >>>

--Ned.



More information about the Python-list mailing list