Gabriel
Gabriel

Reputation: 42329

PEP8 E226 recommendation

The E226 error code is about "missing whitespace around arithmetic operator".

I use Anaconda's package in Sublime which will highlight as a PEP8 E226 violation for example this line:

hypot2 = x*x + y*y

But in Guido's PEP8 style guide that line is actually shown as an example of recommended use of spaces within operators.

Question: which is the correct guideline? Always spaces around operators or just in some cases (as Guido's recommendation shows)?

Also: who decides what goes into PEP8? I would've thought Guido's recommendation would pretty much determine how that works.

Upvotes: 8

Views: 5360

Answers (3)

In this case that happened to me. We should have space always between numbers or variables and operations.

example:

a=b*4 wrong


a = b * 4  correct

Upvotes: 0

Alasdair
Alasdair

Reputation: 308779

The maintainers of the PEP8 tool decide what goes into it.

As you noticed, these do not always match the PEP8 style guide exactly. In this particular case, I don't know whether it's an oversite by the maintainers, or a deliberate decision. You'd have to ask them to find out, or you might find the answer in the commit history.

Guido recently asked the maintainers of pep8 and pep257 tools to rename them, to avoid this confusion. See this issue for example. As a result, the tools are getting renamed to pycodestyle and pydocstyle, respectively.

Upvotes: 9

erip
erip

Reputation: 16935

It says in PEP8:

If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator. (Emphasis is my own).

In the listed example, + has a lower priority, so the BDFL elects to use whitespace around it and uses no whitespace around higher priority *.

Upvotes: 5

Related Questions