Featureintro
Constants by convention
Also called Python constants, UPPER_CASE names, typing.Final, const in Python, how to declare a constant in Python, PEP 8 constants.
Python has no constant declaration. By convention, a module-level name in capital letters, such as MAX_SIZE, is a constant. typing.Final informs type checkers, but Python allows a new assignment.
- Language
- Python
Synopsis
MAX_OVERFLOW = 100
TOTAL = 0
def overflow(n):
return n > MAX_OVERFLOW
print(overflow(150), MAX_OVERFLOW)
True 100
The capital letters tell the reader that MAX_OVERFLOW must not change. Python treats it as an ordinary global variable.
Semantics
r1. PEP 8 says: "Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL."1 Example: convention.
r2. The name is only a convention. A constant name is a normal name, and an assignment can rebind it. Example: rebind.
r3. typing.Final is a "Special typing construct to indicate final names to type checkers. Final names cannot be reassigned in any scope." But "There is no runtime checking of these properties."2 Example: final-not-checked.
r4. A few built-in names are real constants: "The names None, False, True and debug cannot be reassigned (assignments to them, even as an attribute name, raise SyntaxError), so they can be considered “true” constants."3 Example: true-constant.
r5. A constant name does not make its object immutable. "Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable."4 Use an immutable object, such as a tuple or a frozenset, for a constant collection. Example: mutable-constant.
Examples
MAX_SIZE = 10
MAX_SIZE = 20
print(MAX_SIZE)
20
from typing import Final
MAX_SIZE: Final = 9000
MAX_SIZE += 1
print(MAX_SIZE)
9001
A type checker reports an error for MAX_SIZE += 1, but Python runs it (rule r3).
This code raises SyntaxError, on purpose.
None = 0
Pitfalls
A list in a constant name can still change:
This code runs but gives a wrong answer on purpose.
COLORS = ["red", "green"]
COLORS.append("blue")
print(COLORS)
FROZEN = ("red", "green")
print(hasattr(FROZEN, "append"))
['red', 'green', 'blue']
False
The name COLORS still refers to the same list, but the list has a new item. A tuple has no append method (rule r5).
A class attribute in capital letters is also only a convention. Mark it with Final if you use a type checker. The typing documentation says that "Final names declared in class scopes cannot be overridden in subclasses", as a rule for type checkers.2
Comparison
Go has constant declarations: "A constant declaration binds a list of identifiers (the names of the constants) to the values of a list of constant expressions."5 An assignment needs an addressable left operand: "Each left-hand side operand must be addressable, a map index expression, or (for = assignments only) the blank identifier." "The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation", or a field or element of an addressable value.5 A constant is none of these, so a Go compiler rejects an assignment to a constant. Python has no such check. The capital letters are only a message to the reader (rules r1 and r2).
References
- PEP 8 - Style Guide for Python Code, Naming Conventions, Constants
- typing - Support for type hints, Python 3.15 documentation, typing.Final
- Built-in Constants, Python 3.15 documentation, Note after __debug__
- The Python Language Reference, Python 3.15, 3.1. Objects, values and types
- The Go Programming Language Specification, Constant declarations; Assignment statements; Address operators
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag E82DES Edit this page History Report an error Markdown