PEP-8, which is Guido's style guide and generally good to follow, does not completely discourage single-line usage like the example. It's not clear to me how Chris's example fits into the guidelines.

PEP-8:
"While sometimes it’s okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements.
...
# Wrong:
if foo == 'blah': do_blah_thing()
for x in lst: total += x
while t < 10: t = delay()
"

If the one-liner were not in a multi-statement block, it would be all right with PEP-8. OTOH, there is nothing that says one has to fully comply with PEP-8. I personally tend to use

if test: return

even inside larger blocks. If one is working with other someone else's project and there is a style guide, it's important to follow that guide because the other people involved will find it easier to read and understand your code.

If you are working on your own project, PEP-8 is always a good starting point, and flake8 and pylint will be happier. That's worth something.

On 12/14/2022 11:35 PM, Chris Angelico wrote:
On Thu, 15 Dec 2022 at 14:41, Aaron P <transreduction...@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.

Of course this would be considered an anti-pattern, and Flake8 will complain.

Any opinions, or feedback on the matter.

Nothing at all wrong with writing that on a single line. If you have
issues with Flake8 not accepting your choices, reconfigure Flake8 :)

ChrisA

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to