Skip to main content
CPython follows specific coding standards to maintain consistency across the codebase. This guide covers style guidelines for both C and Python code.

Python Code Style

Python code in CPython follows PEP 8 - the official Python style guide.

PEP 8 Essentials

Indentation

Never use tabs for Python code. Configure your editor to use spaces.

Line Length

Naming Conventions

Imports

Whitespace

Documentation Strings

C Code Style

C code in CPython follows PEP 7 - the C style guide.

PEP 7 Essentials

Indentation

Braces

Naming Conventions

Comments

Error Handling

Reference Counting

NULL Checks

Code Organization

File Structure

C files in CPython typically follow this structure:

Type Hints

Use type hints in Python 3.5+ style:

Documentation

Python Documentation

Document modules, classes, and functions:

C Documentation

Use structured comments:

Tools and Automation

Running Style Checks

Editor Configuration

Create .editorconfig in your editor:

Pre-commit Hooks

Consider setting up pre-commit hooks:

Special Builds and Debug Macros

When writing C code for debug builds:
See Misc/SpecialBuilds.txt for more on debug builds.

Common Patterns

Error Propagation

Resource Cleanup

Code Review Checklist

Before submitting code, verify:
  • Follows PEP 8 (Python) or PEP 7 (C)
  • All functions documented
  • Type hints added (Python)
  • Reference counting correct (C)
  • Error handling complete
  • Tests added
  • No trailing whitespace
  • Unix line endings (LF)
  • No tabs in Python code
  • Consistent indentation

Next Steps

Additional Resources