for -- else: what was the motivation?
Dan Stromberg
drsalists at gmail.com
Sat Oct 8 00:13:17 EDT 2022
The else is executed if you don't "break" out of the loop early.
It cuts down on boolean flags.
On Fri, Oct 7, 2022 at 8:40 PM Axy via Python-list <python-list at python.org>
wrote:
> 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)
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list