If you look at programming language introduction sites, you will see that Python is described as a well-behaved language. I get the impression that Python has more restrictions on how to write it than other languages, but it still has a high degree of freedom, and when you actually develop it, a large amount of habits unique to each programmer will remain in the source.
This isn't a very important issue for personal hobbies, but it's a serious issue for work, especially for team development. Once developed, it basically has to be maintained for many years, and maintenance personnel often change during that period. It is very important to define the program rules in the project for later maintenance.
If you want to define the rules for each project, you can create it from scratch. However, since it takes time and cost, we often create coding standards based on PEP8, so I would like to introduce it.
PEP8 is a Python coding standard created based on the idea that "code is read more often than written". By hijacking this convention, we aim to make the code easier to read and to make the style of code created by each programmer consistent.
In addition, PEP8 shows the attitude that it is not always necessary to comply with this agreement. I think it is better to flexibly create rules according to the characteristics of the project.
But be aware that there are times when you need to be inconsistent
That is, this style guide may not apply.
If in doubt, prioritize your judgment.
Look at other examples and decide which one looks best.
PEP8 itself is a convention, but a mechanism is provided that allows you to easily check whether the code can be written according to this convention.
pip install pep8
--Execute Specify the target source code in the argument and hit the pep8 command
pep8 test.py
--Result
test.py:16:1: E302 expected 2 blank lines, found 1
test.py:22:80: E501 line too long (95 > 79 characters)
test.py:27:1: E302 expected 2 blank lines, found 1
test.py:39:1: E302 expected 2 blank lines, found 1
test.py:44:11: E225 missing whitespace around operator
test.py:46:12: E225 missing whitespace around operator
test.py:47:13: E225 missing whitespace around operator
test.py:50:27: W291 trailing whitespace
test.py:51:34: W291 trailing whitespace
test.py:60:45: W291 trailing whitespace
test.py:66:16: E231 missing whitespace after ':'
test.py:74:80: E501 line too long (90 > 79 characters)
test.py:90:10: E231 missing whitespace after ','
test.py:92:56: E231 missing whitespace after ','
test.py:101:1: W391 blank line at end of file
Here are some of the terms and conditions.
―― 1 Indent uses 4 spaces.
--If you want to continue the line, align the wrapped elements vertically. --Align to open brackets
foo = long_function_name(var_one, var_two,
var_three, var_four)
-Or add four spaces to distinguish between arguments and others
def long_function_name(
var_one, var_two, var_three,
var_four):
--A space-friendly indentation method.
--All line lengths up to 79 characters. --Line breaks use parentheses, brackets, and implicit line continuations in curly braces. --You can also use a backslash
with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
file_2.write(file_1.read())
--Top-level functions and classes are defined with two lines apart. --Inside the class, define the method with one line at a time.
--Use utf8
import
--The import statement should normally separate lines
#good:
import os
import sys
#bad:
import sys, os
--Specify the import statement with an absolute path except for exceptions
#good:
import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example
#bad:
from . import sibling
from .sibling import example
--Imports are grouped below. --Standard library --Third party library --Specific to local applications / libraries
--Basically, do not put characters in expressions or sentences
#good:
spam(ham[1], {eggs: 2})
#bad:
spam( ham[ 1 ], { eggs: 2 } )
#good:
foo = (0,)
#bad:
bar = (0, )
#good:
spam(1)
#bad:
spam (1)
#good:
dct['key'] = lst[index]
#bad:
dct ['key'] = lst [index]
#good:
x = 1
y = 2
long_variable = 3
#bad:
x = 1
y = 2
long_variable = 3
--It is usually optional to add a comma at the end. --Exceptionally required when creating a tuple with one element.
#good
FILES = ('setup.cfg',)
https://pep8-ja.readthedocs.io/ja/latest/
Recommended Posts