Does Python have constants?

Share
Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video
02:51

Python doesn't have constants.

The CONSTANT convention

We have two variables here that are named with a fully uppercase naming convention:

>>> NUMBERS = [2, 1, 3, 4, 7, 11, 18]
>>> COORDINATES = (1, 2, 3)

That naming convention is pretty common for constant variables in other programming languages. Sometimes you'll it in Python as well (PEP 8 actually notes this convention).

Variables point to objects

If we point another variable, nums, to first variable, NUMBERS:

>>> nums = NUMBERS

And then we call the append method on this new nums variable:

>>> nums.append(29)

This will mutate the object that nums points to:

>>> nums
[2, 1, 3, 4, 7, 11, 18, 29]

We've changed the list that nums is pointing to. But NUMBERS and nums point to the same object, which means we've changed NUMBERS too!

>>> NUMBERS
[2, 1, 3, 4, 7, 11, 18, 29]

That's in fact, what assignment does.

Variables point to objects in Python and we've pointed two variables to the same object. So when we change nums, NUMBERS seems to change as well because the object which NUMBERS points to has changed (remember there's 2 types of change in Python).

You can see this mutation by stepping through this interactive Python Tutor visualization:

Immutable objects cannot be changed

This COORDINATES variable points to a tuple:

>>> COORDINATES
(1, 2, 3)

Unlike lists, tuples are immutable objects, meaning tuples cannot be changed.

So, if we point the variable p to COORDINATES:

>>> p = COORDINATES

And then we try to mutate the object that p points to, we'll get an error.

For example we can't assign to an index in that tuple:

>>> p[0] = 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

And we can't append to the tuple:

>>> p.append(29)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'

As hard as we might try, there is no way to mutate the tuple that p points to. We can't change tuples because tuples are immutable objects.

Assignment changes what a variable points to

Immutability is about changing objects; it's about mutation, and mutating the objects. But we have two types of change in Python.

Objects are changed through mutations. Variables are changed through assignment statements. Variables point to objects, and you can change which object a variable points to by assigning to that variable.

So, can we take COORRDINATES and assign it to a different tuple?

>>> COORDINATES = (4, 5, 6)
>>> COORDINATES
(4, 5, 6)

We can! There's nothing stopping us from doing this.

So COORDINATES isn't actually a constant variable. And in fact, there's no way to make it a constant variable. Python doesn't have constants.

Python has immutable objects but not constant variables

In Python, we have this concept of immutability. For each object we can ask, is the object mutable (meaning you can change it) or is it immutable (meaning it can't be changed)?

Variables point to objects. There's no way to lock a variable down and stop it from being reassigned. Constant variables in other languages cannot be reassigned. We don't have any equivalent of that in Python. In Python, there's no way to stop a variable from being reassigned.

As long as you're in the same scope as a variable, you can always reassign it. Likewise, as long as an object is mutable, you can always mutate it. However you can make an immutable object, but you cannot make a constant variable.

Series: Assignment and Mutation

Python's variables aren't buckets that contain things; they're pointers that reference objects.

The way Python's variables work can often confuse folks new to Python, both new programmers and folks moving from other languages like C++ or Java.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.

Python Morsels
Watch as video
02:51