Python Style Guide
This style guide aims to maintain code readability, quality, and maintainability.
- Follow a Universal Design approach that aspires to be interpretable to the widest possible number of people
- Follow PEP 8 Guidelines
- Follow standard best practices, e.g S.O.L.I.D, etc
- Use Black to auto-format your code
Include Google-formatted docstrings: Use Google-style docstrings for functions, methods, and classes to provide
clear and concise documentation.
Type hints: Use input and return type hints for functions and methods to improve code readability and facilitate
better tooling support.
Keyword arguments: Prefer using keyword arguments over simple arguments for functions and methods to improve code
Private methods and attributes: Use leading underscores to denote private methods and attributes in classes, and
@propertydecorators when appropriate.
Descriptive names: Use full words in variable and class names instead of abbreviations (e.g.,
blackformatting: Follow PEP8 and
blackcode formatting guidelines to maintain consistency and readability.
Consistent naming conventions: Adopt consistent naming conventions for variables, functions, and classes.
snake_casefor variables and functions (e.g.,
PascalCasefor class names (e.g.,
UPPERCASEfor constants (e.g.,
Keep functions and methods short: Aim to keep functions and methods concise, ideally not exceeding 15-20 lines of code.
Modularize code: Organize code into modules and packages to maintain a clean and organized codebase.
Minimal comments: Avoid comments if possible. Write code that is simple and descriptive (See pt 5) enough that comments are not necessary. If needed, use comments sparingly to provide context or explain complex or non-obvious sections of your code.
Error handling: Use appropriate error handling techniques, such as
exceptblocks, to handle
exceptions and provide meaningful error messages to users.
Write tests: Write unit tests to ensure the correct functioning of your code.
Code reviews: Perform code reviews with team members or peers to maintain a high-quality codebase.