Behavior of the for-else construct

Rob Cliffe rob.cliffe at btinternet.com
Thu Mar 3 20:52:38 EST 2022



On 04/03/2022 00:55, Chris Angelico wrote:
>
> for victim in debtors:
>      if victim.pay(up): continue
>      if victim.late(): break
> or else:
>      victim.sleep_with(fishes)
If you mean "or else" to be synonymous with "else", then only the last 
debtor is killed, whether he has paid up or not, which seems a tad 
unfair (except that if there are no debtors either you will crash with a 
NameError or some random victim will be killed, which seems consistent 
with Mafia modus operandi while also being a trifle unfair.
If (as I suspect) you mean "or else" to mean 'if a break occurred', then 
at least only one debtor is killed, as an example to the others, and no 
Exception will occur in the unlikely event of "debtors" being empty.
Happy fund-raising!
Rob Cliffe
>
> There's something in this.
>
> ChrisA



More information about the Python-list mailing list