Python 3.8+Annotations & typingterm
Final
constants
Marks a variable as a constant — it must not be reassigned after initialization. For static analysis only.
Syntax
from typing import Final
X: Final = value
X: Final[type] = value
Final forbids rebinding the name, not mutating the object: a list marked Final can still be appended to and sorted in place. Only a static checker enforces it — the interpreter happily performs a reassignment, with no runtime error at all. Do not confuse it with the @final decorator, which forbids subclassing a class or overriding a method.
Examples
from typing import Final
MAX_SIZE: Final = 100
PI: Final[float] = 3.14159
print(MAX_SIZE) # → 100
print(PI) # → 3.14159
from typing import Final
class Config:
HOST: Final = 'localhost'
PORT: Final[int] = 8080
print(Config.HOST) # → localhost
print(Config.PORT) # → 8080
See also
Open in the full glossarydocs.python.orgReport an inaccuracy