← Back to list

Effective Python: 2 Follow the PEP 8 Style Guide

Python Enhancement Proposal (PEP) #8 is the style guide for how to format Python code. Here are a few rules we should be sure to follow!

Jenicloun · 2024-03-06 12:50 · 0 claps · 1.6 min read
#effective-python #coding #python #programming #book-review
Open on Medium ↗
Wiki topics: LIT · Literature & Writing 💻 · Programming 📚 · Books & Reading 👗 · Fashion

Effective Python: 2 Follow the PEP 8 Style Guide

Python Enhancement Proposal (PEP) #8 is the style guide for how to format Python code. Here are a few rules we should be sure to follow!

1. Whitespace

  • Use spaces instead of tabs for indentation.
  • Use four spaces for each level of significant indenting.
  • Lines should be 79 characters in length or less.
  • Additional lines should be indented by four extra spaces from their normal indentation level.
  • In a file, functions and classes should be separated by two blank lines.
  • In a class, methods should be separated by one blank line.
  • In a dictionary, put no whitespace between each key and colon, and put a single space before the corresponding value if it fits on the same line.
  • Put one space before and after the = operator in a variable assignment.
  • For type annotations, ensure that there is no separation between the variable name and the colon, and use a space before the type information.

2. Naming

  • Functions, variables, and attributes should be in lowercase_underscore format.
  • Protected instance attributes should be in _leading_undersocre format.
  • Private instance attributes should be in __double_leading_underscore format.
  • Classes (including exceptions) should be in CapitalizedWord format.
  • Module-level constants should be in ALL_CAPS format.
  • Instance methods in classes should use self, which refers to the object, as the name of the first parameter.
  • Class methods should use cls, which refers to the class, as the name of the first parameter.

3. Expressions and Statements

  • Use inline negation (if a is not b) instead of negation of positive expressions (if not a is b).
  • Don’t check for empty containers or sequences (like [] or ‘’) by comparing the length to zero (if len(some_list) == 0). Use if not some_list and assume that empty values will implicitly evaluate to False.
  • The same thing goes for non-empty containers or sequences.
  • Avoid single-line if statements, for and while loops, and except compound statements.
  • If you can’t fit an expression on one line, surround it with parentheses and add line breaks and indentation to make it easier to read.
  • Prefer surrounding multiline expressions with parentheses over using the \ line continuation character.

4. Imports

  • Always put import statements at the top of a file.
  • Always use absolute names for modules when importing them.
  • If you must do relative imports, use the explicit syntax.
  • Imports should be in sections in the following order: standard library modules, third-party modules, and your modules.

[embed]Two ways to import module


메타데이터
post_id
6bfc5ded3c1c
slug
effective-python-2-follow-the-pep-8-style-guide-6bfc5ded3c1c
url
https://medium.com/@jenicloun/effective-python-2-follow-the-pep-8-style-guide-6bfc5ded3c1c
canonical_url
https://medium.com/@jenicloun/effective-python-2-follow-the-pep-8-style-guide-6bfc5ded3c1c
author_url
https://medium.com/@jenicloun
status
ok
fetched_at
2026-07-15 22:34:01