PEP 8 and extraneous whitespace

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 26 20:49:44 EDT 2011


AlienBaby wrote:

>> (on limiting line lengths)..
> 
> I didn't see this mentioned;
> 
> I have a hard time keeping python code limited to 80 char line lengths
> _because_ indentation is significant, 

Are you suggesting that if indentation was not significant, you wouldn't
write such long lines? *wink*

Whether significant or not, the fact that it is there at all is what
matters.

> and can end up consuming quite a lot of linespace itself.

If you are using 8 space indents, do yourself, and your readers, a favour
and change to 4 spaces.

If you regularly have more than four or five nested indents, I would
seriously advise you to refactor your code into a flatter structure. I've
done a quick (and unscientific) survey of my code, and roughly 80% of it is
between 1 and 3 indents (inclusive). Only a tiny portion reaches 6 indents.
If I've ever gone to 7, and I can't find it with a quick random survey.


> Couple that with a liking for 
> long_memorable_explanatory_names, and an 80 char limit causes (me)
> problems.

Names can be too long as well as too short. If you are a carpenter, you
surely wouldn't bang nails with a
metal_manually_operated_hammering_device_with_nail_removing_attachment, you
would use a hammer.

But even with long names, if you are *regularly* going over 80 characters,
you may be doing something wrong. 80 characters is a lot -- most of my
lines of code are under 50, including indentation. I haven't seen your
code, so I could be way off base here, but I expect you're using deeply
hierarchical data structures, and violating the Law of Demeter:

paper = paper_boy.give_newspaper()
# Pay the paper boy.
paper_boy.collect_payment(customer.backpocket.wallet(2.0, tip=300.00))

Don't let the paper boy reach into your wallet and pay himself!



-- 
Steven




More information about the Python-list mailing list