python styles: why Use spaces around arithmetic operators?

P

Peng Yu

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:

Yes:

i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)

No:

i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
 
R

rantingrick

This webpagehttp://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?

The rational is simple. Guido is God and if you don't follow his words
then you will be tortured. His favorite means is by forcing you to
wear Dutch wooden shoes every day whist programming Ruby! ;-)
 
M

Martin P. Hellwig

The rational is simple. Guido is God and if you don't follow his words
then you will be tortured. His favorite means is by forcing you to
wear Dutch wooden shoes every day whist programming Ruby! ;-)

Wat is er mis met klompen?
 
R

rantingrick

Martin said:
Wat is er mis met klompen?

Well specifically their made from wood and wood is a very hard
substance. Also i did not go into detail but he makes sure to pick
shoes that are three sizes too small. You know a good podiatrist can
be tough to come by in these times. It's a pretty severe punishment if
you ask me.
 
S

Steven D'Aprano

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.

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"
 
G

geremy condra

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


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"

This is the rule that I use, with the exception that I will generally
explicitly parenthesize the numerator in a division, since my eyes
frequently gloss over the / symbol for some reason.

Geremy Condra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top