Behavior of the for-else construct

Dieter Maurer dieter at handshake.de
Fri Mar 4 12:08:42 EST 2022


Avi Gross wrote at 2022-3-4 16:43 +0000:
>Your use is creative albeit it is not "needed" since all it does is make sure your variable is initialized to something, specifically None.
>
>So would this not do the same thing?
>
>  eye = None
>
>  for eye in range(0):
>      print(eye)
>
>  eye

It would.

But, the `for` construct's main purpose is often to determine
a value for the loop variable and in this case, I like
that it itself can ensure that it gets a value.


More information about the Python-list mailing list