The Japanese version of PEP8 is at the following URL https://pep8-ja.readthedocs.io/ja/latest/#id4 Although it is written in, I am not good at reading long sentences, so I summarized it in my own way. I tried to summarize one or two sentences per theme, but it is still long, so I divide the article into multiple times. Sample code is also included for the content that was judged to be difficult to explain using only letters. Please comment if you have over-summarized and changed the meaning.
Insert four spaces for each indent.
Space is preferable. In Python3, I get an error when I mix tabs and spaces in indentation.
Limit the length of all lines to a maximum of 79 characters.
As long as it is unified, it does not matter if you start a new line before or after the binary operator. Expressions in paragraphs always break after binary or relational operators, but structured expressions are always recommended to break before binary operators.
Good example: Easy to match operators and operands
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
Bad example: The operator is separated from the operand
income = (gross_wages +
taxable_interest +
(dividends - qualified_dividends) -
ira_deduction -
student_loan_interest)
Leave two lines for each function or class definition. The definition of the function in the class is empty line by line.
You should always use UTF-8. import The import statement should usually be line separated.
Good example
import sys
import os
bad example
import sys,os
However, it is OK in the following cases
from subprocess import Popen, PIPE
__all__,__author__,__version__
Module-level “double underscore variables” such as are written before the import statement
The string enclosed in single quotes'and the string enclosed in double quotes "are the same. It doesn't matter which one you use as long as it is unified.
Good example:
spam(ham[1], {eggs: 2})
bad example:
spam( ham[ 1 ], { eggs: 2 } )
Good example:
if x == 4: print x, y; x, y = y, x
bad example:
if x == 4 : print x , y ; x , y = y , x
Good example:
spam(1)
bad example:
spam (1)
Good example:
dct['key'] = lst[index]
bad example:
dct ['key'] = lst [index]
Good example:
x = 1
y = 2
long_variable = 3
bad example:
x = 1
y = 2
long_variable = 3
-Do not leave extra whitespace at the end of the line ・ Binary operator always puts only one space on both sides -Do not put spaces on either side of =, which is used to indicate that it is a keyword argument or a default parameter.
Good example:
def complex(real, imag=0.0):
return magic(r=real, i=imag)
bad example:
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
-While observing the rule that function annotations do not put a space before the colon, always put a space on both sides of the-> operator.
Good example:
def munge(input: AnyStr): ...
def munge() -> AnyStr: ...
bad example:
def munge(input:AnyStr): ...
def munge()->PosInt: ...
-When combining with an argument annotation that has a default value, put a space before and after =
Good example:
def munge(sep: AnyStr = None): ...
bad example:
def munge(input: AnyStr=None): ...
-Composite statements (putting multiple statements on one line) are generally deprecated. Also, if / for / while and short sentences may be OK on the same line, but compound statements should be placed. NG
Good example:
if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()
bad example:
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()
Recommended Posts