Basics
Constants
All-caps names communicate design intent: this value is configuration that callers should treat as fixed.
Source
MAX_RETRIES = 3
for attempt in range(1, MAX_RETRIES + 1):
print(f"attempt {attempt} of {MAX_RETRIES}")Output
attempt 1 of 3
attempt 2 of 3
attempt 3 of 3Constants are useful when a repeated literal deserves a name at the domain boundary.
Source
API_VERSION = "2026-05"
print(API_VERSION)Output
2026-05Final lets type checkers reject reassignment, but Python still runs ordinary rebinding at runtime.
Source
from typing import Final
MAX_RETRIES: Final = 3
MAX_RETRIES = 5
print(MAX_RETRIES)Output
5Notes
- Python constants are a convention, not a runtime lock.
- Use all-caps names for fixed module-level configuration.
- Add
Finalwhen static tooling should flag accidental rebinding.
See also
- related: Variables
- next depth: Literal and Final
- next depth: Type Hints
Run the complete example
Expected output
attempt 1 of 3
attempt 2 of 3
attempt 3 of 3
2026-05
5
Execution time appears here after you run the example.