How Python Solves const Without a const Keyword
If you come to Python from JavaScript, Java, C#, C++, or another language, one thing stands out right away: Python has no const keyword…
How Python Solves const Without a const Keyword
If you come to Python from JavaScript, Java, C#, C++, or another language, one thing stands out right away: Python has no const keyword. There’s no special syntax for defining constants.

At first, this can feel like a missing feature—but it’s actually a deliberate design choice. Python handles constants differently.
Python doesn’t have a const keyword by design, not because it’s missing a feature. It takes a different approach from languages that include const. Python’s philosophy is to be a simple, expressive tool for solving problems, rather than a language that exposes every possible control mechanism, as seen in more complex languages like C++.
The Confusion Around const
In JavaScript, const is often used to protect a variable from reassignment.
const x = 10
x = 20 // Error: Assignment to constant variable
At first glance, it looks like const makes values completely unchangeable.
However, objects behave differently.
const numbers = [1, 2, 3]
numbers.push(4) // Allowed, array is mutable
numbers = [5, 6] // Error: cannot reassign
This shows an important distinction.
const protects the variable binding, not the object it refers to.
Python makes this distinction explicit and builds its model around it.
Python’s Core Idea: Names and Objects
Python does not think in terms of variables holding values. Instead, it works with names and objects.
- Names point to objects
- Objects define whether their contents can change
x = 10
In this example:
xis a name10is an object
Rebinding a name is always allowed.
x = 20
This behavior is intentional. Reassignment is explicit and easy to reason about.
Mutable vs Immutable — The Key Concept
Understanding mutability is essential to understanding why Python does not need const. For a more complete example, you can see it here.
- Immutable objects cannot be changed after creation. Any operation that appears to modify them actually creates a new object
- Mutable objects can be changed in place. Modifying them affects the original object
For example, if we use JavaScript:
// Immutable example: strings
let text = "hello"
text[0] = "H"
console.log(text) // "hello"
// Mutable example: arrays
const numbers = [1, 2, 3]
numbers.push(4)
console.log(numbers) // [1, 2, 3, 4]
In Python, the behavior is similar.
# Immutable: strings
s = "hello"
# s[0] = "H" # Error: strings are immutable
# Mutable: lists
numbers = [1, 2, 3]
numbers.append(4)
Most bugs that const is meant to prevent come from mutable objects, not from reassignment itself.
How Constants Work in Python
Python uses naming conventions instead of enforced constants.
PI = 3.14159
MAX_RETRIES = 5
Uppercase names communicate intent. They tell other developers that these values should not be reassigned.
Python relies on discipline and readability rather than strict enforcement. In practice, this works well.
Protecting Mutable Data
Problems usually appear when a value is intended to be constant but refers to a mutable object.
CONFIG = {"debug": True}
CONFIG["debug"] = False
This is allowed because:
CONFIGis only a name- The dictionary is mutable
Ways to reduce accidental changes:
- Pass copies of objects to functions
- Use immutable types such as tuples or frozensets
- Use
MappingProxyTypeto create read-only dictionary views
Passing Values to Functions Safely
Immutable values are safe by default.
def add_one(x):
x += 1
n = 10
add_one(n)
print(n) # 10
Mutable values can be modified unintentionally.
def add_item(items):
items.append(4)
numbers = [1, 2, 3]
add_item(numbers)
print(numbers) # [1, 2, 3, 4]
Protecting Data
Copy the object before modifying it.
def add_item(items):
items = items.copy()
items.append(4)
return items
Or use immutable data structures.
def add_item(items):
return items + (4,)
numbers = (1, 2, 3)
Optional: Static Warnings With Final
Python provides optional static hints using Final.
from typing import Final
PI: Final = 3.14159
This does not prevent runtime changes. It only helps tools like linters and editors warn about unintended reassignment.
Why Python’s Approach Works
Python is built on a few simple assumptions:
- Reassigning names is usually harmless
- Mutating objects is where most bugs appear
- Clear and explicit code is better than hidden restrictions
Immutable types, naming conventions, and straightforward patterns remove most of the need for a const keyword.
Controlling Shared State with deepcopy
Most of the confusion around const actually comes from mutable objects, not from reassignment. The real problem is shared state. When two names refer to the same mutable object, changing it through one name affects the other unexpectedly.
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4]
Even if the name a is intended to stay constant, the object it points to can still be changed. This is why Python does not rely on const. A keyword that prevents rebinding would not stop this kind of bug.
Python gives you explicit tools to control this behavior. The copy module lets you make a new, independent object. A shallow copy duplicates the outer container but keeps references to the inner objects. A deep copy duplicates everything recursively, breaking all shared references.
import copy
a = [[1, 2], [3, 4]]
b = copy.deepcopy(a)
b[0].append(99)
print(a) # [[1, 2], [3, 4]]
print(b) # [[1, 2, 99], [3, 4]]
This makes the behavior obvious: mutation only affects the objects you explicitly choose to change. Nothing happens behind the scenes. Python prefers this explicit approach over hidden rules or language-enforced constants.
Takeaways
This design makes Python a simple and expressive tool for most problems. At the same time, it can feel limiting in situations that require very fine-grained control over objects and memory — the kind of control exposed by more complex languages like C++. In Python, understanding the difference between mutable and immutable objects is essential.
메타데이터
- post_id
- f89fcfd0b027
- slug
- how-python-solves-const-without-a-const-keyword-f89fcfd0b027
- url
- https://python.plainenglish.io/how-python-solves-const-without-a-const-keyword-f89fcfd0b027
- canonical_url
- https://python.plainenglish.io/how-python-solves-const-without-a-const-keyword-f89fcfd0b027
- author_url
- https://medium.com/@noryx
- status
- ok
- fetched_at
- 2026-07-13 09:35:19