In Python, class constants are values that are defined within a class and remain constant throughout the lifetime of the class. These constants are typically declared as class variables, and by convention, they are written in all uppercase letters to distinguish them from regular variables.
Python does not provide a built-in way to declare constants (like the final
keyword in Java or C++), but you can simulate the behavior of constants in the following ways.
1. Using Class Variables for Constants
A common approach to define class constants is to define them as class variables. Since Python does not prevent class variables from being changed, the convention is to use uppercase variable names to indicate that they are meant to be constants.
Example:
class MyClass:
PI = 3.14159 # Class constant
MAX_SIZE = 100 # Class constant
# Accessing the class constants
print(MyClass.PI) # Output: 3.14159
print(MyClass.MAX_SIZE) # Output: 100
In the above example, PI
and MAX_SIZE
are considered constants because their values should not change.
2. Using @property
Decorators to Create Read-Only Class Constants
To prevent modification of the class constant (i.e., making it truly immutable), you can use the @property
decorator to define read-only class attributes. This allows you to prevent direct modification while providing a mechanism to access the constant.
Example:
class MyClass:
_PI = 3.14159 # Private variable to store the constant
@property
def PI(cls):
return cls._PI
# Accessing the constant
print(MyClass.PI) # Output: 3.14159
In this example:
_PI
is the private variable holding the constant value.@property
is used to create a getter method that returns the constant value.PI
cannot be modified directly because it is a property.
3. Using enum
to Create Named Constants
If you need to create a set of related constants, you can use Python’s enum
module. This allows you to define an enumeration, where each member represents a constant value.
Example using enum
:
from enum import Enum
class MyConstants(Enum):
PI = 3.14159
MAX_SIZE = 100
MIN_SIZE = 1
# Accessing enum constants
print(MyConstants.PI) # Output: MyConstants.PI
print(MyConstants.PI.value) # Output: 3.14159
- The
Enum
class is used to create a set of constants. Each constant is assigned a name and a value. - You access the constants by referencing their names (e.g.,
MyConstants.PI
), and their values can be accessed via.value
(e.g.,MyConstants.PI.value
).
4. Using dataclasses
for Constants
If you use Python’s dataclass
to define a class, you can declare constants as class variables by simply declaring them as fields of the class. You can also freeze the dataclass to make it immutable.
Example using dataclass
:
from dataclasses import dataclass
@dataclass(frozen=True)
class MyClass:
PI: float = 3.14159
MAX_SIZE: int = 100
# Accessing constants
print(MyClass.PI) # Output: 3.14159
print(MyClass.MAX_SIZE) # Output: 100
Here, the @dataclass(frozen=True)
decorator makes the class immutable (i.e., the constants cannot be changed once set).
5. Making Constants with const
in Python (via Third-Party Libraries)
Although Python doesn’t have built-in support for constants, you can use third-party libraries like const
to simulate constant behavior, where values cannot be modified after being set.
Example with const
library:
pip install const
import const
class MyClass:
const.PI = 3.14159
const.MAX_SIZE = 100
# Accessing constants
print(MyClass.PI) # Output: 3.14159
print(MyClass.MAX_SIZE) # Output: 100
In this case, the const
module ensures that the variables are protected from being changed.