[Python coding standard] PEP 8 vs Google Style

Introduction

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.

note

――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

Comparison table

item PEP8 Google
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,withSentences andassertMay be used in etc. withDo 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, IBan 原則Ban。例外はカウンタ・イテレータのietc,exceptIne, Of the file objectf
Back and forth_Naming _xxx, __xxx, xxx_Mention the case of using If non-public_xxx__xxxIs deprecated
Lambda expression If you assign to a variabledefWrite 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

How to write when joining lines with parentheses

#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 ...

Other things that are mentioned only in the Google Style Guide

--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 ()andfilter () --Nest when thewith 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 moreforstatements 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

Impressions

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

[Python coding standard] PEP 8 vs Google Style
Comply with Python coding standard PEP8
Let's summarize the Python coding standard PEP8 (1)
python> coding guide> PEP 0008 --Style Guide for Python Code
Let's summarize the Python coding standard PEP8 (2)
Comply with Python coding standard PEP8
Apply Google Java Style formatter in IntelliJ
EP 2 Follow the PEP 8 Style Guide
Java 1 1 support from Google App Engine
python> coding guide> PEP 0008 --Style Guide for Python Code
[Python coding standard] PEP 8 vs Google Style
How to automatically check if the code you wrote in Google Colaboratory corresponds to the python coding standard "pep8"
[Python] Standard input
Building an environment to comply with the Python coding standard (PEP8) with Eclipse + PyDev
First Python ~ Coding 2 ~
python> coding guide> PEP 0008 --Style Guide for Python Code
Change the style of matplotlib
Summary about pythonic style (1): PEP8
Apply Google Java Style formatter in IntelliJ
EP 2 Follow the PEP 8 Style Guide
Java 1 1 support from Google App Engine
python> coding guide> PEP 0008 --Style Guide for Python Code
[Python coding standard] PEP 8 vs Google Style
Expose settings.json for efficient Python coding in VS Code
Runtime version of Google App Engine / Python Standard Environment
[Python] About standard input
Python> coding rule> PEP8> read_chpoint.py: 20: 1: E302 expected 2 blank lines, found 1