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
Naming Conventions
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:
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:
Next Steps
Additional Resources