[issue22316] Add rule about "extraneous whitespace around colon" to "Whitespace In Expressions and Statements" of PEP8

Guido van Rossum report at bugs.python.org
Sun Aug 31 23:02:37 CEST 2014


Guido van Rossum added the comment:

Oooh, yes. The colon in a slice should be treated as a binary operator, with equal space before and after.

I think we can add this (as a new bullet following the bullet "Immediately before a comma, semicolon, or colon"):

- However, the colon in a slice acts like a binary operator, and
  should have equal amounts on either side.  In an extended slice,
  both colons must have the same amount of spacing applied. ::

      Yes: ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
      Yes: ham[lower + offset : upper + offset], ham[lower : upper : 3]
      No: ham[1: 9], ham[1 :9]


I'm not sure what to recommend for extended slices when one or several of the slots are empty; my intuition suggests that there should be no spaces around any colons in that case, but I'm not sure what to do if you really have a long expression as one slot. Which is better?

    ham[lower + 1 :: step]

or

    ham[lower + 1 : : step]

similar for other cases, e.g.

    ham[lower + 1 : upper + 1 :]

vs.

    ham[lower + 1 : upper + 1 : ]

To me, *all* of those feel weird.

----------
nosy: +gvanrossum

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22316>
_______________________________________


More information about the Python-bugs-list mailing list