Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W503 enforces breaking after binary operators but PEP-8 appears to disagree #513

Closed
b4hand opened this issue May 27, 2016 · 4 comments
Closed

Comments

@b4hand
Copy link

b4hand commented May 27, 2016

Straight from PEP-8:

Should a line break before or after a binary operator?

For decades the recommended style was to break after binary operators. But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line. Here, the eye has to do extra work to tell which items are added and which are subtracted:

# No: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

To solve this readability problem, mathematicians and their publishers follow the opposite convention. Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations" [3] .

Following the tradition from mathematics usually results in more readable code:

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style is suggested.

Considering that the last paragraph allows breaking before or after binary operators, it seems like this should be handled similar to the --hang-indent option.

@b4hand
Copy link
Author

b4hand commented May 27, 2016

It looks like this was also previously discussed in #197 and #498.

@sigmavirus24
Copy link
Member

Exactly. This was discussed in #498 and has been addressed in two parts. In the future, please search open and closed issues before hand. ;-)

@b4hand
Copy link
Author

b4hand commented May 28, 2016

I initially searched for "W503", but that's found nothing relevant. After filing the ticket, I kept searching for binary operator and that came up with the other results. If nothing else this will be a useful result for others who do a similar search.

@sigmavirus24
Copy link
Member

Also #504 was opened about this with an even less searchable title and body. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants