PEP 8 and extraneous whitespace

Chris Angelico rosuav at gmail.com
Fri Jul 29 06:25:46 EDT 2011


On Thu, Jul 28, 2011 at 4:18 PM, OKB (not okblacke)
<brenNOSPAMbarn at nobrenspambarn.net> wrote:
> Thomas 'PointedEars' Lahn wrote:
>>  Automatic word-wrap, where available, really is not a solution; it
>> is a bad workaround to a problem caused by the original author of
>> the source code that can be easily avoided by them taking more care
>> while coding.
>
>        I think exactly the opposite.  The source file should reflect the
> semantic organization of the code, without extraneous concessions to
> visual display (like "lining things up" with spaces or splitting long
> lines with hard newlines).  The editor should present the code nicely.
> People already argue for this general mindset when they say that "any
> good edit will let you set the tab width", etc.

That mandates that formatting NOT be a part of the language. I could
take C code and reformat it in various ways with a script, and easily
guarantee that the script won't affect the code by simply having it
only ever adjust whitespace. This concept simply won't work in Python.

Personally, I like to be able to format things according to where they
came from, or other criteria. Take a look at this code, for instance;
the post has highlighted the difference between 1 and 0, but otherwise
hasn't changed it:

http://thedailywtf.com/Articles/The-Shapes.aspx

Note how the formatting is beautifully consistent. Each line is
wrapped to a tidy 70 characters, or whatever it is (I haven't
counted). Each data row, therefore, has the same number of characters
in it. But is that useful? Would it not be far more useful to wrap
them each at their image widths? Yes, it might take a bit more
vertical space, but the source code would *look like the image* and
that massively helps readability. This is why a source code tidying
script can't fully grok readability; if a program looks at a sequence
of strings, how can it know where to break them? All of these
(hypothetical) examples are single string literals, broken across
lines:

-----
"This is string 1.\0"
"This is string 2, and it is longer.\0"
"String 3.\0"
-----
"This is a long paragraph of"
"text, which I have wrapped somewhat messily, and"
"which a good text"
"tidying program should be able to clean up"
"for me."
-----
"Usage: programname [options] [argument]\n"
"-f\tFooify\n"
"-b\tBarify\n"
"\n"
"argument should be either '5-minute' or 'course'\n"
-----

Can you write a script that perfectly handles these sorts of literals?
Considering that the divider in the first one might not be \0, but
might be any character or even string of characters, it's not really
possible for an editor to know where to wrap something; which means
that it's up to the programmer to make things readable. And that
demands that the programmer know what he's doing, and that the
programmer have the power to express what he's doing how he chooses.
Overly-strict formatting rules/guides remove that power.

ChrisA



More information about the Python-list mailing list