Whitespace in Expressions and Statements
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies)
Does ham[lower + offset : upper + offset]
violate the above rule?
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies)
Does ham[lower + offset : upper + offset]
violate the above rule?
This often comes up with the Black formatter. Here’s what the Black docs say:
Slices
PEP 8 recommends to treat
:
in slices as a binary operator with the lowest priority, and to leave an
equal amount of space on either side, except if a parameter is omitted (e.g.
ham[1 + 1 :]
). It recommends no spaces around:
operators for “simple expressions”
(ham[lower:upper]
), and extra space for “complex expressions”
(ham[lower : upper + offset]
). Black treats anything more than variable names as
“complex” (ham[lower : upper + 1]
). It also states that for extended slices, both:
operators have to have the same amount of spacing, except if a parameter is omitted
(ham[1 + 1 ::]
). Black enforces these rules consistently.This behaviour may raise
E203 whitespace before ':'
warnings in style guide
enforcement tools like Flake8. SinceE203
is not PEP 8 compliant, you should tell
Flake8 to ignore these warnings.