Single line if statement with a continue

Rob Cliffe rob.cliffe at btinternet.com
Thu Dec 15 08:30:39 EST 2022



On 15/12/2022 04:35, Chris Angelico wrote:
> On Thu, 15 Dec 2022 at 14:41, Aaron P <transreductionist at gmail.com> wrote:
>> I occasionally run across something like:
>>
>> for idx, thing in enumerate(things):
>>      if idx == 103:
>>          continue
>>      do_something_with(thing)
>>
>> It seems more succinct and cleaner to use:
>>
>> if idx == 103: continue.
>>
>>
>> Nothing at all wrong with writing that on a single line. If you have
>> issues with Flake8 not accepting your choices, reconfigure Flake8 :)
>>
>> ChrisA
I'm so glad that Chris and others say this.  It (i.e. if plus 
break/continue/return on a single line) is something I have quite often 
done in my own code, albeit with a feeling of guilt that I was breaking 
a Python taboo.  Now I will do it with a clear conscience. 😁
Best wishes
Rob Cliffe


More information about the Python-list mailing list