From d2c09a26cf9e7d9a5f63fe891f4432b011e04c38 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 31 Aug 2014 20:18:33 -0700 Subject: [PATCH] Clarify space around colon in slice (severa cases). --- pep-0008.txt | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pep-0008.txt b/pep-0008.txt index 982dfe0b5..acb6f5998 100644 --- a/pep-0008.txt +++ b/pep-0008.txt @@ -408,6 +408,27 @@ Avoid extraneous whitespace in the following situations: Yes: if x == 4: print x, y; x, y = y, x No: if x == 4 : print x , y ; x , y = y , x +- However, in a slice the colon acts like a binary operator, and + should have equal amounts on either side (treating it as the + operator with the lowest priority). In an extended slice, both + colons must have the same amount of spacing applied. Exception: + when a slice parameter is omitted, the space is omitted. + + Yes:: + + ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:] + ham[lower:upper], ham[lower:upper:], ham[lower::step] + ham[lower+offset : upper+offset] + ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)] + ham[lower + offset : upper + offset] + + No:: + + ham[lower + offset:upper + offset] + ham[1: 9], ham[1 :9], ham[1:9 :3] + ham[lower : : upper] + ham[ : upper] + - Immediately before the open parenthesis that starts the argument list of a function call:: @@ -417,8 +438,8 @@ Avoid extraneous whitespace in the following situations: - Immediately before the open parenthesis that starts an indexing or slicing:: - Yes: dict['key'] = list[index] - No: dict ['key'] = list [index] + Yes: dct['key'] = lst[index] + No: dct ['key'] = lst [index] - More than one space around an assignment (or other) operator to align it with another.