PEP 8 Compliance ✅

Adhere to PEP 8 for clean, readable code. Key points:

  • Use 4 spaces per indentation level
  • Limit line length to 79 characters
  • Always use spaces around operators (e.g., x = 5 vs x=5)
  • Use lowercase with underscores for function names

Naming Conventions 📜

Follow these naming rules:

  • Variables: snake_case (e.g., user_name)
  • Functions: snake_case (e.g., calculate_total())
  • Classes: PascalCase (e.g., DatabaseConnection)
  • Constants: UPPER_SNAKE_CASE (e.g., MAX_RETRIES)

Code Structure 🧩

  • Use 2 blank lines between top-level functions
  • 1 blank line between method definitions
  • Avoid using ; to separate multiple statements
  • Use docstrings for public APIs

Best Practices 🛠️

  • Always import from the shortest possible path
  • Use list comprehensions for concise code
  • Prefer isinstance() over type checks
  • Keep functions focused and small

Further Reading 📚

For deeper insights, check our Python Best Practices guide.

python_pep8
python_code_structure