Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
PEP 8
Coding Foundations

PEP 8

Fundamental Style Rules for Clean and Readable Python Code

Oleh Lohvyn

by Oleh Lohvyn

Backend Developer

Dec, 2023
4 min read

facebooklinkedintwitter
copy
PEP 8

PEP 8 (Python Enhancement Proposal 8) is a document that establishes standards for coding style in the Python programming language. In this article, we will explore key aspects of PEP 8, its core recommendations, and how adhering to these rules contributes to the creation of clean, readable, and consistent code.

Core Tenets of PEP 8

1. Variable and Function Naming:

  • Use lowercase letters for names, separated by underscores (snake_case).
  • Limit the length of names to 79 characters.

2. Indentation and Whitespace:

  • Use spaces for indentation, not tabs.
  • Use 4 spaces for each level of indentation.

3. Maximum Line Length:

  • Limit the length of a line to 79 characters to ensure readability.
  • For detailed documentation, the length can be extended to 72 characters.

4. Imports:

  • Use separate lines for each import.
  • Avoid using "wildcard" imports.

Run Code from Your Browser - No Installation Required

Run Code from Your Browser - No Installation Required

Сomments and Documentation

5. Comments:

  • Use comments to explain complex sections of code.
  • Avoid obvious comments; code should be self-explanatory.

6. Docstring Documentation:

  • When adding documentation to functions and classes, use docstrings.
  • Adhere to the reStructuredText standard for formatting documentation.

Other Important Recommendations

7. Logical Line Length:

  • Limit the length of logical lines of code to 79 characters.

8. Exceptions:

  • Use exceptions for real exceptional behavior, not for managing program flow.

Final Thoughts

PEP 8 defines rules that facilitate consistent code formatting within the Python community. Adhering to these standards not only improves the readability of your code but also contributes to the creation of a consistent and professional software overall. Strict adherence to PEP 8 is the key to producing clean, readable, and collaborative Python code.

Start Learning Coding today and boost your Career Potential

Start Learning Coding today and boost your Career Potential

FAQs

Q: What specific recommendations does PEP 8 provide regarding the formatting of variable and function names in Python?
A: According to PEP 8, variable and function names should be written in lowercase and separated by underscores (snake_case). The length of names should not exceed 79 characters.

Q: What are the requirements from PEP 8 regarding indentation and whitespace in Python code?
A: PEP 8 recommends using spaces for indentation instead of tabs. It suggests using 4 spaces for each level of indentation.

Q: What does PEP 8 recommend regarding the maximum line length in Python code?
A: PEP 8 recommends limiting the length of a line to 79 characters to ensure readability. For detailed documentation, the maximum length can be extended to 72 characters.

Q: What rules does PEP 8 have for imports in Python?
A: PEP 8 recommends using separate lines for each import statement and avoiding the use of "wildcard" imports, as it can lead to namespace pollution.

Q: What does PEP 8 suggest regarding comments in Python code?
A: PEP 8 suggests using comments to explain complex sections of code. However, it emphasizes avoiding obvious comments, as the code should be self-explanatory.

Q: What are the core principles of PEP 8 regarding docstring documentation in Python?
A: PEP 8 recommends using docstrings for documenting functions and classes. The documentation should adhere to the reStructuredText standard for proper formatting.

¿Fue útil este artículo?

Compartir:

facebooklinkedintwitter
copy

¿Fue útil este artículo?

Compartir:

facebooklinkedintwitter
copy

Contenido de este artículo

We're sorry to hear that something went wrong. What happened?
some-alt