for -- else: what was the motivation?

Weatherby,Gerard gweatherby at uchc.edu
Mon Oct 10 10:13:10 EDT 2022


try:
                open(disk)
except:
                error(“Can’t open disk”)
lots of things

From: Python-list <python-list-bounces+gweatherby=uchc.edu at python.org> on behalf of Karsten Hilbert <Karsten.Hilbert at gmx.net>
Date: Monday, October 10, 2022 at 5:46 AM
To: python-list at python.org <python-list at python.org>
Subject: Re: for -- else: what was the motivation?
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

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
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!nWwvwKC2eetL3YQnTS4jDih5YRZ_ziu4gPuU73R7LDX8-Kq0bXR-h4E_0kJopk-ud2oeAq6NwvykTB40o82ris6pM1aC$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!nWwvwKC2eetL3YQnTS4jDih5YRZ_ziu4gPuU73R7LDX8-Kq0bXR-h4E_0kJopk-ud2oeAq6NwvykTB40o82ris6pM1aC$>


More information about the Python-list mailing list