Good day.

Is it permissible in Python to write very short if blocks and loops in one line? Are there any official recommendations on this issue?

I mean something like one of the following options:

if check: continue if x.param: x.modify() while x.value: x.rotate() 

In my opinion, it looks so much better, especially if several similar short blocks follow each other - otherwise the ugly comb would have been made. Am I right?

  • If you work alone, it is permissible, otherwise we strictly follow the standards. In practice, there are chains with a length of several lines, if you do not break them, then it is difficult to read. This is a short line here, in practice there will be a current comment in the form of text insertion. Further processing. Just not readable. - Igor

1 answer 1

Python syntax allows using such constructs, but in pep8 their use is not recommended:

Compound statements (multiple statements on the same line) are generally discouraged.

Yes:

 if foo == 'blah': do_blah_thing() do_one() do_two() do_three() 

Rather not:

 if foo == 'blah': do_blah_thing() do_one(); do_two(); do_three()