for / while else doesn't make sense

Chris Angelico rosuav at gmail.com
Sat May 21 00:01:52 EDT 2016


On Sat, May 21, 2016 at 1:50 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, 21 May 2016 10:24 am, Jon Ribbens wrote:
>
>> On 2016-05-20, Ethan Furman <ethan at stoneleaf.us> wrote:
>
>>> If you don't take the extra step of _break_ it is the usual case.
>>
>> Having an "for: else:" clause without a "break" would be so unusual
>> that it's literally nonexistent, because it would always be a bug.
>> So no, it isn't the usual case for "for: else:".
>
>
> What do you mean? A for...else without a break is perfectly legal code, and
> does *EXACTLY* what it is documented as doing:
>
> - first the "for" block runs, looping as appropriate;
> - THEN the "else" block runs, *once*.
>
> How is this "always a bug"?
>
> Would you classify the second line here:
>
> print("Hello World!")
> pass
>
>
> as a bug? What exactly would your bug report be? "pass statement does
> nothing, as expected. It should do nothing. Please fix."
>

Yes, I would. It's not a bug in Python or CPython - it's a bug in the
second line of code there. It implies something that isn't the case.
It's like having this code:

if True:
    pass
elif False:
    pass
else:
    assert True

It's well-defined code. You know exactly what Python should do. But is
it good code, or is it the sort of thing that gets posted here:

http://thedailywtf.com/articles/a-spiritual-journey

ChrisA



More information about the Python-list mailing list