# ugly but legitimate indentation
def foo():
print("Hello, World!")
foo()
Hello, World!
PEP stands for Python Enhancement Proposal, a design document providing information to the Python community, or describing a new feature for Python or its processes. PEP 8, in particular, is Python’s style guide, namely a set of rules that specify how to format Python code for maximum readability.
Python’s philosophy emphasizes readability, which is one of the key reasons for its widespread adoption. Readability has guided the development of Python’s style guide, known as PEP 8. In this section, we outline some of the PEP 8 guidelines and show how they affect the writing of clean & professional Python code.
Python uses indentation to define blocks of code. In other words, the Python interpreter assigns a semantic value to indentation. This is different from other programming languages where indentation has solely an aesthetic role, as e.g. in C++.
1 space represents the minimum discriminant between two different levels of indentation, although PEP 8 recommends using 4 spaces per indentation level to ensure consistency and readability across various text editors and development environments.
# ugly but legitimate indentation
def foo():
print("Hello, World!")
foo()
Hello, World!
# best practice
def foo():
print("Hello, World!")
foo()
Hello, World!
For sake of comparison, in C++ and in most of other programming languages, the {
and }
parentheses wrapping around a block of code define its scope, so the indentation doesn’t affect the program’s logic.
#include <iostream>
using namespace std;
void foo() {
<< "Hello, World!" << endl;
cout }
() foo
The code above works exactly like the one below:
#include <iostream>
using namespace std;
void foo() {
<< "Hello, World!" << endl;
cout }
() foo
On the contrary, in Python, incorrect indentation can lead to errors or unintended behavior. For example:
def foo():
# Incorrectly indented
print("Hello, World!")
Cell In[3], line 3 print("Hello, World!") ^ IndentationError: expected an indented block after function definition on line 1
Lines should not exceed 79 characters to ensure code readability and compatibility with various devices and editors. For longer expressions, one must use line breaks. Tools like Flake8 can be employed to enforce this limit and help maintain code quality.
Blank lines must be used to separate functions and classes, and within classes to separate methods. This helps in visually organizing the code and making it easier to navigate.
Imports are to be grouped into three categories: standard library modules, third-party modules, and your own modules. Each group should be placed in separate blocks, sorted alphabetically. The module isort
automatizes said task.
import os
import sys
from third_party import third_party_library
from . import calc
Extraneous whitespace characters are to be avoided in the following situations:
# Correct use of whitespace
1], {eggs: 2})
spam(ham[# Incorrect use of whitespace
1], {eggs: 2} )
spam( ham[1] , {eggs: 2} )
spam( ham[1] , { eggs: 2 } ) spam( ham[
Automatic reformatting can be achieved using linters like Black.
One should stick to the convention of using CamelCase for classes and snake_case for functions and variables.
class MyClassName:
def my_function_name(variable_name):
pass
Usage of descriptive naming styles for variables, functions, classes, and modules should be in place. Avoidance of names that are too vague or too verbose is preferable.
# Too vague
def calc(p, t):
return p + (p * t)
# Too verbose
def calculate_total_price_in_the_case_of_increased_inflation_from_previous_quarter(price, tax_rate):
return price + (price * tax_rate)
# More descriptive and concise
def calculate_total_price(price, tax_rate):
return price + (price * tax_rate)
Integrating the list of briefly mentioned formatting tools, the following tools can help one adhere to PEP 8 guidelines, including:
import
statements
Comments
Comments should be complete sentences. If a comment is a phrase or sentence, its first word should be capitalized, unless it’s an identifier that begins with a lower-case letter.