Skip to content

pep8

  1. Indentation: Use 4 spaces per indentation level. Avoid using tabs.
  2. Maximum Line Length: Limit all lines to a maximum of 79 characters for code, and 72 for docstrings/comments. However, the recommended line length for code is 88 characters.
  3. Imports: Imports should usually be on separate lines and should be grouped in the following order:
    • Standard library imports
    • Related third-party imports
    • Local application/library specific imports
  4. 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
    • Before a colon used for a slice
    • No whitespace immediately before or after a binary operator.
  5. Comments: Write comments that are clear and concise. Comments should be complete sentences and should be used sparingly.
  6. Naming Conventions: Follow consistent naming conventions:
    • Use lowercase with underscores for function and variable names (e.g., my_function, my_variable).
    • Use CapWords (or CamelCase) for class names (e.g., MyClass).
  7. 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
    • Before a colon used for a slice
    • No whitespace immediately before or after a binary operator.
  8. Blank Lines: Use blank lines sparingly to separate logical sections of code, but don’t overuse them.
  9. Encoding: Code should be written in UTF-8.
  10. Imports Formatting: Imports should be at the top of the file, and each import should be on a separate line. Imports should also be organized.
  11. 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
    • Before a colon used for a slice
    • No whitespace immediately before or after a binary operator.
  12. Comments: Write comments that are clear and concise. Comments should be complete sentences and should be used sparingly.
  13. Naming Conventions: Follow consistent naming conventions:
    • Use lowercase with underscores for function and variable names (e.g., my_function, my_variable).
    • Use CapWords (or CamelCase) for class names (e.g., MyClass).

These are some of the key points from PEP 8. Adhering to PEP 8’s recommendations can make your Python code more readable and maintainable, and it’s considered a best practice in the Python community. Many code editors and IDEs have built-in or available tools to help enforce PEP 8 style guidelines.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)