Single line if statement with a continue

dn PythonList at DancesWithMice.info
Sat Dec 17 16:59:14 EST 2022


On 16/12/2022 02.30, Rob Cliffe via Python-list wrote:
> 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. 😁

Anxiety doesn't help anyone - least of all you.

Remember another Python mantra: "we're all adults here"!

(also (compulsory interjection) PEP-008 is not a set of rules for all 
Python code - see PEP-008)

-- 
Regards,
=dn


More information about the Python-list mailing list