Fundamentals 7 min read

Python Coding Style Guidelines (Applicable to Django)

This guide presents comprehensive Python coding conventions—including indentation, line length, quoting, blank lines, encoding, import ordering, spacing, line‑break handling, comment styles, docstring rules, and naming standards—providing clear examples and best‑practice recommendations for clean, maintainable code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Coding Style Guidelines (Applicable to Django)

This document outlines a complete set of Python coding style rules that improve readability and maintainability, and are also suitable for Django projects.

Indentation : Use four spaces per indentation level.

Line length : Keep lines under 80 characters (max 120 in special cases). This aids side‑by‑side diffs, console viewing, and highlights design issues.

Quotes : Use single quotes for machine identifiers, double quotes for natural language strings, triple double quotes for docstrings, and raw strings for regular expressions.

Blank lines : Separate module‑level functions and class definitions with two blank lines; separate class methods with one blank line. Multiple blank lines can separate logical groups of functions.

Encoding : Files should be UTF‑8 encoded; a coding header is optional for Python 3.

Import statements : Write each import on its own line, place imports after module docstrings, use absolute imports, order groups with a blank line between them, and prefer explicit imports.

import os import sys import msgpack import zmq import foo

Spaces : Surround binary operators with a single space, avoid spaces inside parentheses or brackets, and do not add extra spaces around default‑value equals signs.

# Correct spacing i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b)

Line breaks : For long statements, break after commas, align subsequent lines with the opening parenthesis, or use a backslash for continuation. Avoid multiple statements on one line.

foo = long_function_name(var_one, var_two, var_three, var_four)

Comments : Block comments start with # followed by a space and are separated by blank lines; line comments should be preceded by at least two spaces from the code.

# Block comment # Another block comment

Docstrings : Follow PEP 257 – every public module, class, function, and method should have a docstring, and the closing triple quotes should be on a line by themselves unless the docstring is a single line.

"""Return a foobar Optional plotz says to frobnicate the bizbaz first. """ """Oneline docstring"""

Naming conventions : Avoid ambiguous single‑character names, use lowercase with underscores for modules and functions, CamelCase for classes, uppercase with underscores for constants, and add a trailing underscore to avoid clashes with Python keywords.

These guidelines collectively form a practical style guide for writing clean, consistent Python code.

Pythonbest practicesDjangoprogramming fundamentalscoding stylepep8
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.