Basics
Constants
Python has no const keyword for ordinary names. Modules use all-caps names such as MAX_RETRIES to say “treat this as fixed configuration, not changing state.”
The interpreter will still let code rebind the name. That is why constants are primarily an API and readability convention. If a project also uses static typing, Final can make the convention machine-checkable.
Named constants remove magic values from code and give repeated literals one place to change.
All-caps names communicate design intent: this value is configuration that callers should treat as fixed. Comparing against MAX_RETRIES instead of a bare 3 keeps the meaning beside the check, and gives the limit one place to change.
Source
MAX_RETRIES = 3
attempts = 2
print(attempts < MAX_RETRIES)
attempts = 3
print(attempts < MAX_RETRIES)Output
True
FalseConstants 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
True
False
2026-05
5
Execution time appears here after you run the example.