Utilizing PEP 8 Guidelines for Cleaner Python Code

A detailed guide on how to apply the PEP 8 style guide in your Python projects to ensure clean, consistent, and effective code writing.

0 likes
11 views

Rule Content

# Utilizing PEP 8 Guidelines for Cleaner Python Code

## Context

This rule applies to all Python projects to ensure code readability and maintainability by adhering to the PEP 8 style guide.

## Requirements

- **Code Layout**
  - Use 4 spaces per indentation level.
  - Limit all lines to a maximum of 79 characters.
  - Separate top-level function and class definitions with two blank lines.
  - Separate method definitions inside a class with a single blank line.
  - Surround top-level function and class definitions with two blank lines.
  - Use blank lines sparingly inside functions to indicate logical sections.

- **Imports**
  - Place imports at the top of the file, after any module comments and docstrings.
  - Group imports in the following order:
    1. Standard library imports
    2. Related third-party imports
    3. Local application/library-specific imports
  - Use absolute imports whenever possible.
  - Avoid wildcard imports (`from module import *`).

- **Whitespace in Expressions and Statements**
  - Avoid extraneous whitespace in the following situations:
    - Immediately inside parentheses, brackets, or braces.
    - Between a trailing comma and a following close parenthesis.
    - Immediately before a comma, semicolon, or colon.
    - Immediately before the open parenthesis that starts the argument list of a function call.
    - Immediately before the open parenthesis that starts an indexing or slicing.
    - More than one space around an assignment (or other) operator to align it with another.

- **Comments**
  - Use comments to explain the reasoning behind code decisions.
  - Write comments in complete sentences, capitalized and punctuated.
  - Use inline comments sparingly and only when necessary.
  - Place inline comments on the same line as the statement they refer to, separated by at least two spaces.

- **Naming Conventions**
  - Use `snake_case` for function and variable names.
  - Use `PascalCase` for class names.
  - Use `UPPER_CASE` for constants.
  - Avoid using names that are too general or too wordy.
  - Use a single leading underscore to indicate a non-public method or variable.
  - Use a double leading underscore to invoke name mangling.

- **Programming Recommendations**
  - Use `is` for comparisons to `None`.
  - Use `is not` for negative comparisons to `None`.
  - Avoid using `==` to compare to `None`.
  - Use `if not seq` to check for empty sequences.
  - Use `if seq` to check for non-empty sequences.
  - Avoid using `if len(seq) == 0` to check for empty sequences.
  - Avoid using `if len(seq) > 0` to check for non-empty sequences.

## Examples

### Good Example

def calculate_area(width, height):
    """Calculate the area of a rectangle."""
    return width * height
### Bad Example

def calculateArea( width, height ):
    return width*height
## References

- PEP 8 – Style Guide for Python Code: https://peps.python.org/pep-0008/
- How to Write Beautiful Python Code With PEP 8: https://realpython.com/python-pep8/