Let's summarize the Python coding standard PEP8 (1)

Introduction

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.

Code layout

Indent

Insert four spaces for each indent.

Tabs or spaces?

Space is preferable. In Python3, I get an error when I mix tabs and spaces in indentation.

1 line length

Limit the length of all lines to a maximum of 79 characters.

Should I break before or after the binary operator?

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)

Blank line

Leave two lines for each function or class definition. The definition of the function in the class is empty line by line.

Source file encoding

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

Module-level double underscore variable name

__all__,__author__,__version__

Module-level “double underscore variables” such as are written before the import statement

Quotation marks in the string

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.

Whitespace characters in expressions and sentences

Whitespace is superfluous in the following cases

-Immediately after the beginning and end of parentheses, brackets, and curly braces

Good example:
spam(ham[1], {eggs: 2})
bad example:
spam( ham[ 1 ], { eggs: 2 } )

・ Commas, semicolons, just before colons

Good example:
if x == 4: print x, y; x, y = y, x
bad example:
if x == 4 : print x , y ; x , y = y , x

-Immediately before the opening parenthesis that starts the argument list of the function call

Good example:
spam(1)
bad example:
spam (1)

-Immediately before the opening bracket of the index or slice

Good example:
dct['key'] = lst[index]
bad example:
dct ['key'] = lst [index]

• Insert one or more spaces around the operators to align the assignment operators

Good example:
x = 1
y = 2
long_variable = 3
bad example:
x             = 1
y             = 2
long_variable = 3

・ Other recommendations

-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

Let's summarize the Python coding standard PEP8 (1)
Let's summarize the Python coding standard PEP8 (2)
Comply with Python coding standard PEP8
[Python coding standard] PEP 8 vs Google Style
Building an environment to comply with the Python coding standard (PEP8) with Eclipse + PyDev
[Python] Adjusted the color map standard
Let's summarize the degree of coupling between modules with Python code
[Blender x Python] Let's master the material !!
Python3 standard input I tried to summarize
Let's read the RINEX file with Python ①
Let's observe the Python Logging cookbook SocketHandler
[Python] Summarize the rudimentary things about multithreading
python> coding guide> PEP 0008 --Style Guide for Python Code
Python amateurs try to summarize the list ①
Let's summarize the construction of NFS server
From Python 3.4, pip becomes the standard installer! ??
Japanese translation: PEP 20 --The Zen of Python
How to automatically check if the code you wrote in Google Colaboratory corresponds to the python coding standard "pep8"
Let's parse the git commit log in Python!
[Python] Let's execute the module regularly using schedule
[Python] Standard input
Let's summarize Squid
First Python ~ Coding 2 ~
A python amateur tries to summarize the list ②
Let's summarize Apache
Summarize Python import
Sort in Python. Next, let's think about the algorithm.
HoloViews may become the standard for Python visualization tools
Have python parse the json entered from the standard input
(◎◎) {Let's let Python do the boring things) ......... (Hey? Let's let Python do the homework} (゜) (゜)
Let's use the Python version of the Confluence API module.
Let's use the open data of "Mamebus" in Python
[Python] Let's remember the new writing style after pip10
[Python] Let's change the URL of the Django administrator site
I tried to summarize the string operations of Python
Find the maximum Python
the zen of Python
[Python] Split the date
[Python] About standard input
Let's review the language specifications around Python iterators and generators
How to debug the Python standard library in Visual Studio
Change the standard output destination to a file in Python
What you can do with the Python standard library statistics
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
(Python3) No. oO (Are you using the standard library?): 5 shaders
Python> coding rule> PEP8> read_chpoint.py: 20: 1: E302 expected 2 blank lines, found 1