for -- else: what was the motivation?

Weatherby,Gerard gweatherby at uchc.edu
Wed Oct 12 17:11:48 EDT 2022


As did I.

tree = ET.parse(lfile)
            for child in tree.getroot():
                if child.tag == 'server':
                    break
            else:
                raise ValueError(f"server tag not found in {lfile}")

I think there are other places I could be using it, but honestly I tend to forget it’s available.

From: Python-list <python-list-bounces+gweatherby=uchc.edu at python.org> on behalf of Stefan Ram <ram at zedat.fu-berlin.de>
Date: Wednesday, October 12, 2022 at 2:22 PM
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. ***

Axy <axy at declassed.art> writes:
>So, seriously, why they needed else if the following pieces produce same
>result? Does anyone know or remember their motivation?

  Just wrote code wherein I used "else"! This:

import locale
for name in( 'de', 'de_DE', 'deu_deu', 'deu', 'German', 'Deutsch' ):
    try: locale.setlocale( locale.LC_ALL, name ); break
    except locale.Error: pass
else: print( "Programm kann deutsche Schreibweise nicht einrichten." )

  .


--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iyDac-XjNlj78G0XwNzZ-FEHyuCZIy33n3cI9MUDM_FnEdR04mSQ5Ln0OA1ETUNloyH24iY9meNHVdixLgWRYL8$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iyDac-XjNlj78G0XwNzZ-FEHyuCZIy33n3cI9MUDM_FnEdR04mSQ5Ln0OA1ETUNloyH24iY9meNHVdixLgWRYL8$>


More information about the Python-list mailing list