python styles: why Use spaces around arithmetic operators?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Jul 26 19:31:23 EDT 2010


On Mon, 26 Jul 2010 17:20:09 -0500, Peng Yu wrote:

> This webpage http://www.python.org/dev/peps/pep-0008/ recommends the
> following. It looks to me that both styles are fine. Could anybody let
> me know what the rationale is behind this recommendation?
> 
>     - Use spaces around arithmetic operators:

Because it looks better and is easier to read. Operators are small 
(single characters) and sometimes need space around them to stand out.

>           i=i+1

See? It's hideously ugly and cramped. It's much worse if you use larger 
names:

sensiblynamedvariable=sensiblynamedvariable+1

But use your common sense. I usually group powers, multiplications and 
divisions, and separate additions and subtractions:

y = 2*x + 1 - (3*x - 4/(2 + x**2))**-2

And unary + and - operators should always be grouped with their operand:

y = -2  # not "- 2"



-- 
Steven



More information about the Python-list mailing list