by: Cobena Isaac

How to Define Constants in Python

How to Define Constants in Python

In programming, we often deal with values that should never change — like the value of π (pi), the speed of light, or a tax rate used across an application.

These are called constants — fixed values that represent something that stays the same throughout a program’s execution.

What Is a Constant?

A constant is simply a variable that’s intended to remain unchanged once defined throughout a program’s execution.

Unlike some languages (like Java or C++), Python does not have a built-in constant type. There’s no rule that stops you from reassigning a constant, but developers rely on convention and discipline to signal that certain values should not be modified.

Python Constant Naming Convention

By convention, constants are written in uppercase letters, and words are separated using underscores for readability.

Here are a few examples:

1
2
3
4
PI = 3.14159
GRAVITY = 9.81
SPEED_OF_LIGHT = 299792458
MAX_USERS = 1000

Even though you can technically reassign them:

1
PI = 3.14  # Allowed, but strongly discouraged

It’s considered bad practice to do so. Constants are meant to be treated as unchangeable, and reassigning them can make your code confusing or error-prone.

Organizing Constants in a Separate File

For larger projects, it’s common to organize constants into a separate module, and name it as constants.py.

This keeps your codebase clean and makes constants easy to maintain or update.

Here’s how it looks:

1
2
3
4
# constants.py
PI = 3.14159
GRAVITY = 9.81
SPEED_OF_LIGHT = 299792458

Then, in your main script, you can import and use them:

1
2
3
4
5
# main.py
import constants

print(constants.PI)
print(constants.GRAVITY)

Output:

1
2
3.14159
9.81

This approach keeps your constants centralized and avoids duplication across files.


Making Constants Truly Immutable

You can simulate true immutability using several techniques that prevent accidental modification.

The most robust approach is using the Enum class from the enum module:

1
2
3
4
5
6
7
8
from enum import Enum

class Constants(Enum):
    PI = 3.14159
    GRAVITY = 9.81

# Accessing constant values
print(Constants.PI.value)  # Output: 3.14159

In this example, we can assess the PI constant value by - `Constants.PI.value.

Using an Enum ensures that values are fixed — once defined, they cannot be reassigned or modified. Attempting to modify an Enum value will raise an AttributeError:

1
Constants.PI.value = 3.14  # Raises AttributeError

You can also use other methods such as:

  • Named tuples for structured immutable data
  • Custom classes with property decorators to block reassignment
  • Frozen dataclasses for complex immutable objects

For creating constants.


Conclusion

While Python doesn’t enforce constants, you can follow naming conventions and good coding practices to make your intent clear.

  • Use UPPERCASE names for constants
  • Avoid reassigning them
  • Store them in a dedicated file like constants.py

Following these conventions makes your code more readable, maintainable, and professional — and that’s what writing good Python is all about. ✨

Thank for reading!

Your next step is to learn about Python Special Variables