Got curious about PEP8, so I looked it up and decided to organize it
References: [Book] Effective Python
Python Enhancement Proposal 8 (PEP8)
- A style guide that tells you how to organize Python code
- An agreement to write code in a specific format
- Using a consistent style makes maintenance easier and improves readability!
- An agreement to write code in a specific format
- Detailed information is available at python.org!
In Python, whitespace is syntactically significant
Python programmers are particularly sensitive to the impact of whitespace because of code clarity!
-
Indent with spaces, not tabs
-
Use 4 spaces for each level of syntactically significant indentation
-
Each line should be 79 characters or less
-
When an expression continues to the next line, use 4 additional spaces beyond the normal indentation level
-
Functionsandclassesin a file should be separated by two blank lines -
Methods within a Class should be separated by a single blank line
-
Do not use spaces around
list indices,function calls, orkeyword argument assignments
PEP 8 suggests unique naming conventions for different parts of the language
This makes it easy to distinguish the type corresponding to each name when reading code!
Functions,variables, andattributesfollow the lowercase_underscore formatProtectedinstance attributes follow the _leading_underscore formatPrivateinstance attributes follow the __double_leading_underscore format- Classes and Exceptions follow the CapitalizedWord format
- Module-level constants follow the ALL_CAPS format
- The first parameter of a
Class instancemethod (which references the object) should be named self - The first parameter of a
Class method(which references the class) should be named cls
The Zen of Python states: "There should be one-- and preferably only one --obvious way to do it"
PEP 8 codifies this style as the standard for expressions and statements
- Use inline negation (
if a is not b) instead of negation of positive expressions (if not a is b) - Don't check for empty values (
[]or'') by checking the length (if len(somelist) == 0)- Use
if not somelistand assume that empty values are implicitly False - The same approach applies to non-empty values!
- Non-empty values will make
if somelistimplicitly True
- Non-empty values will make
- Use
- Don't write single-line
if statements,for and while loops, orexcept compound statements- Write these statements on multiple lines for clarity!
- Always place import statements at the top of the file
- When importing modules, always use the module's absolute name; do not use relative path names based on the current module's path
- ex) Use
from bar import fooinstead of justimport foo!
- ex) Use
- If you must use relative imports, use explicit syntax:
from . import foo - Imports should be organized in the order of
standard library modules,third-party modules, andyour own modules- Within each subsection, import in alphabetical order
Install and use pylint and PEP8!
$ pip install pylint$ pip install pep8