Skip to content

Commit 843b7d2

Browse files
committed
A few extra clarifications about multiline if and with statements. Also,
conform the multiline-if statement conditionals to other recommendations in the PEP, i.e. that lines in a multiline conditional end with the binary operator.
1 parent b147896 commit 843b7d2

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

pep-0008.txt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,34 @@ Optional::
121121
var_one, var_two,
122122
var_three, var_four)
123123

124-
This PEP explicitly takes no position on how (or whether) to further
125-
visually distinguish continuation lines in the header from the nested suite
126-
in an ``if`` statement, where the combination of a two character keyword, a
127-
single space and an opening parenthesis creates a natural 4-space indent for
128-
the expression in the header. Some acceptable options in this situation
129-
include::
124+
.. _`multiline if-statements`:
125+
126+
When the conditional part of an ``if``-statement is long enough to require
127+
that it be written across multiple lines, it's worth noting that the
128+
combination of a two character keyword (i.e. ``if``), plus a single space,
129+
plus an opening parenthesis creates a natural 4-space indent for the
130+
subsequent lines of the multiline conditional. This can produce a visual
131+
conflict with the indented suite of code nested inside the ``if``-statement,
132+
which would also naturally be indented to 4 spaces. This PEP takes no
133+
explicit position on how (or whether) to further visually distinguish such
134+
conditional lines from the nested suite inside the ``if``-statement.
135+
Acceptable options in this situation include, but are not limited to::
130136

131137
# No extra indentation.
132-
if (this
133-
and that):
138+
if (this_is_one_thing and
139+
that_is_another_thing):
134140
do_something()
135141

136142
# Add a comment, which will provide some distinction in editors
137143
# supporting syntax highlighting.
138-
if (this
139-
and that):
144+
if (this_is_one_thing and
145+
that_is_another_thing):
140146
# Since both conditions are true, we can frobnicate.
141147
do_something()
142148

143-
144149
# Add some extra indentation on the conditional continuation line.
145-
if (this
146-
and that):
150+
if (this_is_one_thing
151+
and that_is_another_thing):
147152
do_something()
148153

149154
The closing brace/bracket/parenthesis on multi-line constructs may
@@ -231,9 +236,12 @@ multiple ``with``-statements cannot use implicit continuation, so
231236
backslashes are acceptable::
232237

233238
with open('/path/to/some/file/you/want/to/read') as file_1, \
234-
open('/path/to/some/file/being/written', 'w') as file_2:
239+
open('/path/to/some/file/being/written', 'w') as file_2:
235240
file_2.write(file_1.read())
236241

242+
(See the previous discussion on `multiline if-statements`_ for further
243+
thoughts on the indentation of such multiline ``with``-statements.)
244+
237245
Another such case is with ``assert`` statements.
238246

239247
Make sure to indent the continued line appropriately. The preferred

0 commit comments

Comments
 (0)