A well-known Python coding standard is the standard library code standard PEP 8, but the [Google Python Style Guide](https: /) /google.github.io/styleguide/). So I've summarized how Google Style differs from PEP 8. From the conclusion, it was almost the same as PEP 8, so I omitted the common rules (especially blank lines and blanks). Although there are differences, there may be some points that cannot be covered, but please forgive me.
――Both Japanese translations are available, but they may not be the latest, so I referred to the English version as much as possible. --PEP 8 is in the public domain, Google Style Guide is CC by 3.0
item | PEP8 | |
---|---|---|
Writing multiple sentences on one line | Should stop in principle | if foo: bar(foo) Iselse を伴わなければOK。それ以外IsNG。; Don't use too. |
Maximum length of one line | 79 characters | 80 characters |
Maximum length of one line exception | 99 characters is acceptable with the agreement of the team, 72 characters in the comment line anyway | Exceptions to import statements, URLs and pathnames |
Line continuation\ |
Parentheses are preferable,with Sentences andassert May be used in etc. |
with Do not use unless the statement has 3 or more lines |
Use of docstring | non-Not required for public methods, write others | non-Write except for public, short and obvious methods |
docstring style | None (The content to be written in PEP257 is described) | Yes |
Content of comment | Don't write trivial things | Do not explain the code itself, follow the English grammar, spelling, and punctuation |
String quotes'``" |
either will do | Decide which one to use in the project in principle |
Multi-line character string (excluding docstring) | When using triple quotes""" |
Principle on strings' In the project that decided to use''' But it's okay. There are also references to other writing styles |
TODO comment style | None | Yes |
Import order | Standard library-> external library-> local library | Almost the same as PEP8, but clearly stated in dictionary order |
1 character variable | l , O , I Ban |
原則Ban。例外はカウンタ・イテレータのi etc,except Ine , Of the file objectf |
Back and forth_ Naming |
_xxx , __xxx , xxx_ Mention the case of using |
If non-public_xxx 、__xxx Is deprecated |
Lambda expression | If you assign to a variabledef Write in |
ワンライナー用に使え。Lambda expression部分が60-Use the nested function when it exceeds 80 characters.operator モジュールにある関数をLambda expressionで書くな |
return | If you use it, return in all cases, write something after the return | No mention |
Consistency | Those who are too particular about their hearts are narrow | Be consistent |
#OK with PEP8 or Google
#Align to open brackets
foo = long_function_name(var_one, var_two,
var_three, var_four)
meal = (spam,
beans)
#OK with PEP8 or Google
#hanging indent (4 spaces)
foo = long_function_name(
var_one, var_two, var_three,
var_four)
meal = (
spam,
beans)
#Allowed for PEP8, NG for Google (2 spaces)
foo = long_function_name(
var_one, var_two,
var_three, var_four)
#Not mentioned in PEP8, OK in Google
foo = {
long_dictionary_key: value1 +
value2,
...
}
foo = {
long_dictionary_key:
long_dictionary_value,
...
}
#No mention in PEP8, NG in Google
foo = {
long_dictionary_key:
long_dictionary_value,
...
}
#OK for PEP8, no mention for Google?
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
#OK in PEP8, no mention in Google, maybe NG
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
#I think Google likes this style of writing. PEP8 is also OK.
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f')
--Line break before binary operator in PEP8, no mention in Google --PEP8 exemplifies the description when the condition of the if statement becomes long, Google does not mention it --Google stipulates notation when using function annotation, PEP8 does not mention ――Personally, I like hanging indent because it requires less effort and I can make effective use of one line. --But I think it's easier to see the function arguments in open parentheses ...
--Leave one line between the class
definition statement and the first method
--ʻIf name == Use'main' --Consider splitting when the function exceeds 40 lines --Use
+to concatenate two strings. Use
%,
str.format (),
f-string,
str.join () to join more than that. --Don't combine
map ()and
filter () --Nest when the
with statement is exactly two lines, don't use
\ --Shebang does not have to be written, it is meaningless to write in a file that is not executed directly --Don't write useless
(). Let's write
()to specify the tuple. --Do not use two or more
forstatements or two or more control statements in the comprehension / generator expression. --Don't use
@staticmethodin principle, let's make it a module level function --Use
@ classmethod` for methods used as named constructors or methods that change the global state of a class
--Don't use mutables in the definition of default arguments
--Avoid global variables and use constants
--How to write type annotation
Google, write the same thing as PEP 8 orz, it was hard to read everything. It was good that I decided how to write docstring and type annotation in quite detail.
Recommended Posts