for -- else: what was the motivation?
Jon Ribbens
jon+usenet at unequivocal.eu
Mon Oct 10 11:43:00 EDT 2022
On 2022-10-10, Calvin Spealman <cspealma at redhat.com> wrote:
> On Sat, Oct 8, 2022 at 5:35 PM rbowman <bowman at montana.com> wrote:
>> On 10/7/22 21:32, Axy wrote:
>> > So, seriously, why they needed else if the following pieces produce same
>> > result? Does anyone know or remember their motivation?
>>
>> In real scenarios there would be more logic in the for block that would
>> meet a condition and break out of the loop. If the condition is never
>> met, the else block runs. To steal from w3schools:
>>
>>
>> fruits = ["apple", "peach", "cherry"]
>> for x in fruits:
>> print(x)
>> if x == "banana":
>> break
>> else:
>> print("Yes we got no bananas")
>>
>
> I wonder if for/else could have been less confusing if it was referred to
> as for-break-else and if the else clause was only valid syntax if the for
> loop actually contained a break statement in the first place.
Watch out, I suggested that here some years ago and it was derided
as being an "arrogant and foolish" idea.
More information about the Python-list
mailing list