white space in expressions and argument lists

J

John Salerno

A minor concern, but I'm curious if there is any kind of best practice
for using whitespace within expressions and argument lists in the Python
world. For example:

1 + 2 or 1+2

func(1,2) or func(1, 2)

To me, the space makes it nicer and more readable, but I was surprised
to read in Guido's blog that he thinks 1+2 should be the normal way to
write it. What does everyone else think?
 
L

Lawrence Oluyede

John Salerno said:
1 + 2 or 1+2

func(1,2) or func(1, 2)

I prefer and use 1 + 2 and func(1, 2)
I don't do whitespaces only in argument defaults like

func(foo=baz)
 
S

Sybren Stuvel

John Salerno enlightened us with:
To me, the space makes it nicer and more readable, but I was
surprised to read in Guido's blog that he thinks 1+2 should be the
normal way to write it. What does everyone else think?

I usually write 1 + 2 and func(a, b). Sometimes I even use more spaces
to align stuff:

foo = 'bar'
foobar = 42
azimov = 3

Sybren
 
J

John Salerno

Sybren said:
John Salerno enlightened us with:

I usually write 1 + 2 and func(a, b). Sometimes I even use more spaces
to align stuff:

foo = 'bar'
foobar = 42
azimov = 3

Sybren

Guido listed a few rules that he'd like to see implemented in 2.5, and
one of them was no more than one consecutive white space. I don't know
how realistic some of those suggestions are, but they seem to be getting
a little to restrictive.
 
S

Sybren Stuvel

John Salerno enlightened us with:
Guido listed a few rules that he'd like to see implemented in 2.5,
and one of them was no more than one consecutive white space. I
don't know how realistic some of those suggestions are, but they
seem to be getting a little to restrictive.

I just read the PEP where my way of alignment is under a 'NO' header.
Too bad, since I think it can make things a lot clearer.

Sybren
 
R

rtilley

John said:
All of it was a joke?

You'd have to ask Guido that :)

I took it literally when I first read it b/c it made sense to me and I
did not notice the date. I don't think it will ever be _required_ of all
Python hackers, but I may be wrong.
 
S

Scott David Daniels

Sybren said:
I just read the PEP where my way of alignment is under a 'NO' header.
Too bad, since I think it can make things a lot clearer.

One reason is such code changes too much on code edits:
foo = 'bar'
foobar = 42
azimov = 3
to:
foo = 'bar'
something = 42
azimov = 3

which makes code differences hard to read.

--Scott David Daniels
(e-mail address removed)
 
S

Sybren Stuvel

rtilley enlightened us with:
I took it literally when I first read it b/c it made sense to me and
I did not notice the date. I don't think it will ever be _required_
of all Python hackers, but I may be wrong.

Well, part of it is a serious PEP. It being _required_ was the joke.

Sybren
 
S

Sybren Stuvel

Scott David Daniels enlightened us with:
One reason is such code changes too much on code edits, which makes
code differences hard to read.

Good point. I'll keep it in mind :)

Sybren
 
M

Magnus Lycka

Sybren said:
Scott David Daniels enlightened us with:


Good point. I'll keep it in mind :)

Think particularly about using version management systems
and applying patches coming from different sources etc.

Touching more lines of code than you actually need will
both make it difficult for a reviewer to understand what
has changed (although good diff tools can be told to
ignore pure whitespace changes) and it will also increase
the risk of conflicts in the version management system if
several people are editing the same code.

Finally, if you end up with something like...

a = 1
b = 5
df = 7
ew = 5
qw = 7
a2 = 5
a4 = 7
d3 = 5
df = 7
this_is_a_very_long_variable_name_indeed = 42

....it's *not* easier to see what the value of
e.g. a4 is than if you had just used one space
between the variable name and the =.

As usual, Guido is right, even if it isn't
obvious at first unless you are Dutch. ;^)
 
S

Scott David Daniels

Magnus said:
.... Touching more lines of code than you actually need will
both make it difficult for a reviewer to understand what
has changed (although good diff tools can be told to
ignore pure whitespace changes) ...
Such features have a nasty interaction with languages that use
significant whitespace; you might miss a re-blocking change while
suppressing the hassles from the extra "prettifying" spaces.

--Scott David Daniels
(e-mail address removed)
 
S

Sybren Stuvel

Magnus Lycka enlightened us with:
Think particularly about using version management systems and
applying patches coming from different sources etc.

I was :)
Finally, if you end up with something like...

a = 1
b = 5
df = 7
ew = 5
qw = 7
a2 = 5
a4 = 7
d3 = 5
df = 7
this_is_a_very_long_variable_name_indeed = 42

I won't. It was just an example. I write my code so it can easily be
read, not simply to blindly follow rules.
As usual, Guido is right, even if it isn't obvious at first unless
you are Dutch. ;^)

I'm Dutch.

Sybren
 

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

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,120
Latest member
Natbelix

Latest Threads

Top