Single line if statement with a continue

Chris Angelico rosuav at gmail.com
Thu Dec 15 01:56:08 EST 2022


On Thu, 15 Dec 2022 at 16:29, Thomas Passin <list1 at tompassin.net> wrote:
>
> 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.

Not sure what your point is about it being "in" a multi-statement
block - PEP 8 has nothing to say about that. What it's saying is that
you shouldn't do this:

if foo == 'blah': one(); two(); three()

And I agree; if you're putting more than one statement after your
'if', it's generally clearest to have it on multiple lines. But a
simple "continue" or "break" statement works just fine on the same
line as the if.

ChrisA


More information about the Python-list mailing list