Home / Fundamentals

# How to Create Constants in Python

In programming, a constant is a value that cannot be changed or modified once it has been set. Constants can be useful for storing values that are used frequently throughout a program, but do not need to be modified.

There are a few different ways to create constants in Python, each with its own set of benefits and drawbacks.

## Method 1: Using a Global Variable and the constants Module

One way to create a constant in Python is to use a global variable and the constants module. The constants module is a third-party library that allows you to define global variables as constants, which cannot be modified once they have been set.

To use the constants module, you will need to install it first. You can do this using pip, the Python package manager:

```pip install constants
Once the constants module is installed, you can import it and use it to define a global variable as a constant:

```python
import constants

constants.PI = 3.14159
print(constants.PI)  # Output: 3.14159

# This will raise an error, as constants cannot be modified
constants.PI = 3.14
```

One advantage of this method is that it allows you to define multiple constants at once, by simply assigning multiple values to global variables. However, it is important to note that this method is not foolproof, as it is still possible to modify a constant using globals() or other methods.

## Method 2: Using a NamedTuple from the collections Module

Another way to create constants in Python is to use a NamedTuple from the collections module. A NamedTuple is a subclass of a Python tuple that allows you to access elements using named fields, rather than indexes.

To use a NamedTuple as a constant, you will need to import the collections module and define a NamedTuple with the desired values:

```from collections import NamedTuple

class Constants(NamedTuple):
PI: float = 3.14159
E: float = 2.71828

constants = Constants()
print(constants.PI)  # Output: 3.14159

# This will raise an error, as NamedTuples are immutable
constants.PI = 3.14
```

One advantage of this method is that it is more foolproof, as NamedTuples are immutable and cannot be modified. However, it is not possible to define multiple constants using a single NamedTuple, as you will need to create a separate NamedTuple for each set of constants.

## Method 3: Using an Enum from the enum Module

A third way to create constants in Python is to use an Enum from the enum module. An Enum is a class that allows you to define a set of named constants, each with a unique value.

To use an Enum as a constant, you will need to import the enum module and define an Enum with the desired values:

```from enum import Enum

class Constants(Enum):
PI = 3.14159
E = 2.71828

print(Constants.PI.value)  # Output: 3.14159

# This will raise an error, as Enum values are immutable
Constants.PI.value = 3.14
```

One advantage of this method is that it is very foolproof, as Enum values are immutable and cannot be modified. Additionally, it is possible to define multiple constants using a single Enum, which can be useful for organizing related constants together.

## Method 4: Using a FrozenDict from the frozendict Module

A fourth way to create constants in Python is to use a FrozenDict from the frozendict module. A FrozenDict is a subclass of a Python dictionary that is immutable and cannot be modified once it has been created.

To use a FrozenDict as a constant, you will need to install the frozendict module and import it, then define a FrozenDict with the desired values:

```!pip install frozendict

from frozendict import FrozenDict

constants = FrozenDict({
"PI": 3.14159,
"E": 2.71828
})

print(constants["PI"])  # Output: 3.14159

# This will raise an error, as FrozenDicts are immutable
constants["PI"] = 3.14
```

One advantage of this method is that it allows you to define multiple constants in a single object, which can be useful for organizing related constants together. However, it is not as foolproof as the Enum method, as it is still possible to modify the values of the keys in the FrozenDict using methods such as update() or setdefault().