5. Style
The observation of Python's authors is that programs are more often read
than written. Therefore, they have adopted a specific style guide.
If you're writing Pythhon code, you should adher to it, because
-
others that see your programs will nag about it
-
if you're used to the style, it will be easier to read programs by others
There are some strange choices in the PEP8 standard. They make programs
needlessly long and, for a beginning Python programmer, harder to read.
They are also inconsistent in a number of ways.
This chapter does not explain the complete PEP8. I threw my code
through an on-line PEP8 checker and these are the errors that I found
in my code and my observation of them.
5.1. Spaces
Equal signs must have spaces around them, for example:
a = 1+1
Unless, the equal sign is used for an assignment of named arguments.
Other operators should not have spaces around them.
A comma should have a space after it.
a=function(arg1, arg2)
And parentheses do not get spaces.
The colon that is used for block starts should also not get a space:
def function(arg):
And comments start with
'# '
(hash-space).
5.2. Line length
Maximum line length is 80. Why? Because the IBM punch-card format, introduced
in 1928 had 80 columns.
5.3. Indent
One of the horrors of Python is it's attitude towards indents. Indents
are used to mark blocks. Indents are 4 spaces. Not a tab, four spaces.
Together with the 80-column limit, this also means a maximum
for nesting blocks (19), but if you hit that limit, you should
restructure your code anyway.