Skip to content

Files

Latest commit

53c7491 · Nov 6, 2024

History

History
64 lines (51 loc) · 2.75 KB

python.md

File metadata and controls

64 lines (51 loc) · 2.75 KB

Python Programming Style Guide

Buy Me A Coffee   Ko-Fi   PayPal   Stripe

This style guide provides guidelines for writing Python code that is readable, maintainable, and consistent.

Table of Contents

  1. Introduction
  2. Naming Conventions
  3. Code Formatting
  4. Comments
  5. Function and Method Definitions
  6. Imports
  7. Exceptions
  8. Whitespace
  9. Miscellaneous

Naming Conventions

  • Use lowercase_with_underscores for variable and function names.
  • Use CAPITALIZED_WITH_UNDERSCORES for constants.
  • Use CamelCase for class names.
  • Avoid single-character names, unless they have a well-defined meaning.
  • Avoid using reserved keywords as names.

Code Formatting

  • Use 4 spaces for indentation (avoid tabs).
  • Limit lines to a maximum of 79 characters.
  • Use a single space around operators and after commas.
  • Use blank lines to separate logical sections of code.

Comments

  • Use comments to explain non-obvious code behavior.
  • Write self-explanatory code that reduces the need for comments.
  • Use complete sentences and proper grammar in comments.

Function and Method Definitions

  • Use descriptive names for functions and methods.
  • Add a docstring to describe the purpose, inputs, and outputs of the function.
  • Limit the number of arguments to keep functions concise.

Imports

  • Import modules on separate lines, grouped in the following order:
    1. Standard library imports.
    2. Third-party library imports.
    3. Local application imports.
  • Avoid using wildcard imports (from module import *).

Exceptions

  • Catch specific exceptions instead of using a bare except clause.
  • Avoid catching and swallowing exceptions silently.

Whitespace

  • Use a single blank line to separate logical sections of code.
  • Use blank lines sparingly within functions to improve readability.
  • Avoid excessive whitespace at the end of lines or empty lines at the end of files.

Miscellaneous

  • Be consistent with the style guide within a project.
  • Write code that is easy to understand and maintain.
  • Refactor code to improve readability and eliminate code duplication.

This style guide is inspired by the PEP 8 style guide with a few additional recommendations.