PEP_8_guidelines.md

November 28, 2023 ยท View on GitHub

Pythonic Conventions and Best Practices: PEP 8 Guidelines

PEP 8, or Python Enhancement Proposal 8, is the style guide for writing Python code. It provides a set of rules and best practices intended to improve the readability and consistency of Python code.

Key PEP 8 Guidelines

  1. Indentation: Use 4 spaces per indentation level.

  2. Maximum Line Length: Limit all lines to a maximum of 79 characters for code and 72 for comments and docstrings.

  3. Blank Lines: Use blank lines to separate functions and classes, and larger blocks of code inside functions.

  4. Imports: Imports should usually be on separate lines and are best placed at the top of the file. The order of imports should be standard library modules, third-party modules, and then local application/library specific imports.

    import os
    import sys
    
    from subprocess import Popen, PIPE
    
  5. Whitespace in Expressions and Statements: Avoid extraneous whitespace.

    # Correct:
    spam(ham[1], {eggs: 2})
    
    # Wrong:
    spam( ham[ 1 ], { eggs: 2 } )
    
  6. Comments: Comments should be complete sentences and should be used to explain the intention behind the code.

  7. Naming Conventions:

    • Function Names: Functions should be lowercase, with words separated by underscores.
    • Variable Names: Follow the same convention as function names.
    • Class Names: Class names should follow the UpperCaseCamelCase convention.
    • Constants: Constants should be written in all capital letters with underscores separating words.
  8. Line Breaks and Binary Operators: Break lines before binary operators.

    # Correct:
    income = (gross_wages
              + taxable_interest
              + (dividends - qualified_dividends)
              - ira_deduction
              - student_loan_interest)
    
  9. Code Layout: Avoid trailing whitespace anywhere, and always end a file with a single newline.

  10. Documentation Strings: For public modules, functions, classes, and methods, documentation strings (docstrings) are important. They should follow the multi-line docstring format.

Conclusion

PEP 8 provides a foundation for writing Python code in a clear and consistent manner. Adhering to these guidelines helps maintain code quality and allows other developers to understand and contribute to your code more easily. While it's beneficial to follow PEP 8, it's also important to know when to be flexible. The key is readability and consistency in your codebase.