PEP 8 (Python Enhancement Proposal 8) is the official style guide for writing Python code. It provides conventions for code layout, naming, and best practices to improve code readability and maintainability. It was authored by Guido van Rossum, Barry Warsaw, and Nick Coghlan in 2001.
Following PEP 8 ensures consistency across Python projects, making code easier to read and collaborate on. This is especially important in large teams and open-source projects.
PEP 8 Guidelines
1. Code Layout
- 1.1 Indentation
- Use 4 spaces per indentation level;
- No tabs; only spaces should be used for indentation;
- Continuation lines should align wrapped elements properly;
- 1.2 Maximum Line Length
- Limit all lines to 79 characters;
- For docstrings or comments, limit to 72 characters;
- 1.3 Blank Lines
- Use two blank lines to separate top-level function and class definitions;
- Use one blank line to separate methods inside a class;
- Surround class methods with a single blank line;
2. Imports
- Each import should be on a separate line;
- Use absolute imports instead of relative imports when possible;
- Order imports in the following order:
1. Standard library modules;
2. Third-party modules;
3. Local application imports;
- Use a blank line between different groups;
- Avoid wildcard imports (from module import *), as they clutter the namespace;
3. Naming Conventions
PEP 8 provides a standard for naming variables, functions, classes, and constants.
- 3.1 Variables and Functions
- Use lowercase_with_underscores;
- Functions should be named descriptively;
- 3.2 Class Names
- Use CapWords (PascalCase) for class names;
- 3.3 Constants
- Use UPPERCASE_WITH_UNDERSCORES for constants;
- 3.4 Module and Package Names
- Use short, lowercase names;
- Use underscores only if needed for better readability;
4. Whitespace Usage
- Avoid extra spaces in expressions;
- No spaces around assignment in keyword arguments;
- Use one space after commas and around operators;
5. Comments and Docstrings
- 5.1 Comments
- Comments should be clear, concise, and relevant;
- Use # for inline comments;
- Use complete sentences;
- Keep comments up to date;
- 5.2 Docstrings
- Use triple double-quotes (""") for module, function, and class docstrings;
- The first line should be a short summary;
- Multi-line docstrings should have an empty line before details;
6. Best Practices for Readability
- 6.1 Avoid Complex Expressions
- Break down complex statements into multiple lines;
- 6.2 Avoid Unnecessary Parentheses
7. Avoid Using Unnecessary Constructs
- 7.1 Using is vs. ==
- Use is for identity comparison (e.g., None, True, False);
- Use == for value comparison;
- 7.2 Avoid Mutable Default Arguments
- Default arguments should be immutable;
8. Exception Handling
- Use try-except blocks properly;
- Avoid catching broad exceptions like except:;
PEP 8 is a fundamental guide for writing clean, readable, and maintainable Python code. By following these conventions, developers can improve the consistency of their code and make it easier for others to understand. Many tools like flake8, pylint, and black help enforce PEP 8 compliance automatically.