wxl24life wxl24life - 5 months ago 29
Python Question

Does PEP require whitespace around operators in function arguments?

I have this code:

some_list = range(a, b+1)


After checking my coding style with pep8 plugin for vim, I got this warning:

missing whitespace around operator


It seems that to be compliant with PEP 8 I should instead write this?

some_list = range(a, b + 1)


But I have read PEP 8 - Style Guide for Python Code several times and just can't find the rule applied to the warning above.

So I want to know: when using PEP-8 style, is whitespace needed around operators(+,-,*,/,etc) in a function's arguments?

Answer

http://www.python.org/dev/peps/pep-0008/#other-recommendations

Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).

The exception to that is when = is used to set named parameters.

Edit:

I've looked through the source code of Python's standard library and found an occurrence of the scenario presented above:

http://hg.python.org/cpython/file/9ddc63c039ba/Lib/json/decoder.py#l203

            end = _w(s, end + 1).end()
Comments