Skip to main content

How to Define a Constant in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Python has no const keyword. A constant is an ordinary variable whose name is written in capital letters at the top of a module, such as ISSUER = "https://login.example.org", which tells every reader not to change it.

Syntax​

Syntax
<NAME> = <value>
PartWhat it does
<NAME>The constant's name, in capital letters with underscores between words, such as MAX_RETRIES.
<value>The value the program uses everywhere and never changes.

Python's style guide, PEP 8, sets the convention: "Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL."

The Example​

The example defines two constants, uses them, and then assigns a new value to one of them, to show what Python does about it.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which defines ISSUER and MAX_RETRIES at the top, prints ISSUER and MAX_RETRIES times 2, then assigns a new value to ISSUER and prints it again.

main.py
ISSUER = "https://login.example.org"
MAX_RETRIES = 3

print(ISSUER)
print(MAX_RETRIES * 2)

ISSUER = "https://elsewhere.example.org"
print(ISSUER)

Step 2: Run It​

Terminal
uv run python main.py
Output
https://login.example.org
6
https://elsewhere.example.org

A zsh terminal where uv run python main.py prints https://login.example.org, 6, and then https://elsewhere.example.org after the reassignment.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • Both constants worked like any variable: ISSUER printed its URL, and MAX_RETRIES * 2 printed 6.
  • Assigning a new value to ISSUER worked without an error. Python doesn't enforce constants. The capital letters are a promise between programmers, not a rule the language checks.

That's why the convention matters: a reader who sees ISSUER knows every part of the program expects the same value, and that changing it means changing the program.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting Python to stop a changeThe new value silently replaces the old oneTreat capital-letter names as read-only, and let a type checker help
Defining a constant inside a functionIt's created again on every call and other functions can't see itDefine it at the top of the module
Using a list as a constantThe name stays the same, but anyone can still add to the listUse a tuple for a fixed sequence

Troubleshooting​

SymptomLikely causeFix
A "constant" has an unexpected valueSome code assigned to itSearch the code for NAME =
NameError when a function uses the constantThe constant is defined after the code that runs, or in another module without an importDefine it at the top, or import it

FAQ​

Is there a way to make Python enforce a constant?​

Not at run time. The typing module has Final, which the documentation calls a "Special typing construct to indicate final names to type checkers": a type checker reports an assignment to a Final name, but Python still runs it.

Where do I put constants that several modules share?​

In one module, for example settings.py, and import them from there.

Conclusion​

A Python constant is a module-level name in capital letters. Python won't stop anyone from changing it, so the name itself does the job: it tells every reader the value is fixed.

Additional Resources​