for -- else: what was the motivation?

Rob Cliffe rob.cliffe at btinternet.com
Wed Oct 12 18:19:39 EDT 2022


I too have occasionally used for ... else.  It does have its uses. But 
oh, how I wish it had been called something else more meaningful, 
whether 'nobreak' or whatever.  It used to really confuse me.  Now I've 
learned to mentally replace "else" by "if nobreak", it confuses me a bit 
less.
Rob Cliffe

On 12/10/2022 22:11, Weatherby,Gerard wrote:
> 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