I have some code like this. Should the break occur before the periods or after?
# before
my_var = somethinglikethis.where(we=do_things).where(we=domore).where(we=everdomore)
# this way
my_var = somethinglikethis.where(we=do_things) \
.where(we=domore) \
.where(we=everdomore)
# or this way
my_var = somethinglikethis.where(we=do_things). \
where(we=domore). \
where(we=everdomore)
PEP 8 recommends using parenthesis so that you don’t need
\, and gently suggests breaking before binary operators instead of after them. Thus, the preferred way of formatting you code is like this:The two relevant passages are this one from the Maximum Line Length section:
… and the entire Should a line break before or after a binary operator? section:
Note that, as indicated in the quote above, PEP 8 used to give the opposite advice about where to break around an operator, quoted below for posterity: