[Python-Dev] Suggested addition to PEP 8 for context managers

Guido van Rossum guido at python.org
Tue Apr 17 16:21:46 CEST 2012


On Tue, Apr 17, 2012 at 12:14 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 16 April 2012 17:10, Nam Nguyen <bitsink at gmail.com> wrote:
>> PEP 8 suggests no extra spaces after and before square brackets, and
>> colons. So code like this is appropriate:
>>
>> a_list[1:3]
>>
>> But I find it less readable in the case of:
>>
>> a_list[pos + 1:-1]
>>
>> The colon is seemingly lost in the right.
>>
>> Would it be better to read like below?
>>
>> a_list[pos + 1 : -1]
>>
>> Any opinion?
>
> It says no space *before* a colon, not after. So the following should
> be OK (and is what I'd use):
>
> a_list[pos + 1: -1]

I hope that's now what it says about slices -- that was meant for dict
displays. For slices it should be symmetrical. In this case I would
remove the spaces around the +, but it's okay to add spaces around the
: too. It does look odd to have an operator that binds tighter (the +)
surrounded by spaces while the operator that binds less tight (:) is
not. (And in this context, : should be considered an operator.)

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list