why won't slicing lists raise IndexError?
Chris Angelico
rosuav at gmail.com
Mon Dec 4 19:13:05 EST 2017
On Tue, Dec 5, 2017 at 10:50 AM, Rick Johnson
<rantingrickjohnson at gmail.com> wrote:
> Chris Angelico wrote:
>> <rantingrickjohnson at gmail.com> wrote:
>> > 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.
>>
>> ... how is it superfluous?
>
> If the only purpose of an if/else logic structure is to
> process an arbitrary value _only_ when that value is
> "truthy", and futhermore, the else clause does nothing of
> any significance (and in the example provided, appears to
> be nothing but a placeholder for dead code) then why
> bother writing the else clause in the first place?
Ahhh, I see how it is. You didn't run the code, ergo you don't
understand it. Makes perfect sense. :)
Hint: Truthiness is fairly clearly defined here, regardless of the
value of item.
ChrisA
More information about the Python-list
mailing list