What is pep8?
PEP 8 is Python's official style guide, which outlines the recommended coding conventions for writing clean, readable, and consistent Python code. "PEP" stands for "Python Enhancement Proposal," and PEP 8 is specifically focused on the style and formatting aspects of code. Adhering to PEP 8 can significantly enhance the maintainability and collaboration of Python projects.
Key Points from PEP 8:
- Indentation: Use 4 spaces per indentation level. Avoid using tabs for indentation.
- Line Length: Keep lines of code to a maximum of 79 characters. If necessary, you can extend to 99 characters for comments and docstrings.
- Import Statements: Each import statement should be on a separate line. Import should usually be at the top of the file, after any module-level docstrings.
- Whitespace in Expressions and Statements: Avoid extraneous whitespace within expressions and statements. Use whitespace to improve readability.
- Comments: Use comments to explain non-obvious code. Keep comments concise and to the point.
- Function and Variable Names: Use lowercase words separated by underscores for function and variable names. Use descriptive names that convey the purpose of the entity.
- Constants: Use uppercase letters and underscores for constants. For example, "MAX_COUNT = 100".
- Whitespace in Functions and Control Structures: Use one space on either side of operators and after commas. Use blank lines to separate logical sections within functions.
- Docstrings: Use triple double quotes (""") for multi-line docstrings. Include a brief summary, followed by more detailed explanations if needed.
- Whitespace in Expressions and Statements: Avoid extraneous whitespace within expressions and statements. Use whitespace to improve readability.
- Imports: Imports should usually be on separate lines. Import standard library modules first, followed by third-party modules, and finally your own modules.
- Blank Lines: Use blank lines to separate logical sections within functions and classes. Two blank lines are used between top-level function and class definitions.