for -- else: what was the motivation?

Karsten Hilbert Karsten.Hilbert at gmx.net
Mon Oct 10 05:44:42 EDT 2022


Am Sun, Oct 09, 2022 at 09:58:14AM +0000 schrieb Stefan Ram:

>   I often follow this rule. For me, it's about readability. Compare:
>
> if not open( disk ):
>     error( "Can't open disk" )
> else:
>     printf( "now imagine there's some larger block here" )
		... ad infinitum ....

Should this not be

	if not open( disk ):
	    error( "Can't open disk" )
	else:
		do_lots_of_things_with(disk)

as for readability ?

Or even

	if not open( disk ):
	    error( "Can't open disk" )
	    return what_needs_to_be_returned

	do_lots_of_things_with(disk)

The latter version may need some code reorganization, though.

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B


More information about the Python-list mailing list