for -- else: what was the motivation?
Axy
axy at declassed.art
Mon Oct 10 05:54:44 EDT 2022
On 09/10/2022 03:33, Jach Feng wrote:
> Axy 在 2022å¹´10月8æ—¥ 星期å
上åˆ11:39:44 [UTC+8] çš„ä¿¡ä¸å¯«é“:
>> Hi there,
>>
>> this is rather a philosophical question, but I assume I miss something.
>> I don't remember I ever used else clause for years I was with python and
>> my expectation was it executed only if the the main body was never run.
>> Ha-ha! I was caught by this mental trap.
>>
>> So, seriously, why they needed else if the following pieces produce same
>> result? Does anyone know or remember their motivation?
>>
>> Just curious.
>>
>> Axy.
>>
>> print('--- with else')
>>
>>
>> for i in [1,2,3]:
>> print(i)
>> else:
>> print(4)
>>
>> for i in []:
>> print(i)
>> else:
>> print(5)
>>
>> print('--- without else')
>>
>> for i in [1,2,3]:
>> print(i)
>> print(4)
>>
>> for i in []:
>> print(i)
>> print(5)
> The else is always coming with the break, not the for.
However, the compiler does not complain.
> There are [for ...], [for...break...], and[for...break...else],
That's implied and contradicts Zen of Python, I think. If "else" came
with "break" there had to be a strong indication of that, namely
indentation, as it takes place for all other statements with their
clauses. However, there's no such an explicit connection between "break"
and "else". That's the point.
Well, sorry for this addition to the discussion which went weird way. I
should had to be cautious mentioning particular coding style, that's a
totally different subject, actually. Let's close it at last.
> but the [for...else] is insane.
Not in Python.
Axy.
More information about the Python-list
mailing list