Python Constants
In Python, constants are values that remain unchanged throughout the execution of a program. They provide a way to store data that should not be modified during runtime.
While Python doesn't have built-in support for constants, there are conventions and techniques that can be employed to achieve constant-like behavior.
In this module, we will learn about the various approaches to defining and using constants in Python, along with code examples to help you along the way.
Using Uppercase Naming Convention
Python programmers conventionally use uppercase letters and underscores (_) to denote constants.
Although Python does not enforce the immutability of these variables, adhering to this naming convention serves as a visual cue for developers to treat them as constants. Let's see an example:
>>> PI = 3.14159
>>> RADIUS = 10
>>> area = PI * (RADIUS ** 2)
>>> print("The area of the circle is:", area)
In the code snippet above, PI and RADIUS are considered constants. By using uppercase letters, it is clear that these values should not be modified.
Using the enum Module
Python provides the enum module, which allows us to define enumerated constants. Enumerations are a set of named values that represent a finite set of possible choices. Here's an example:
>>> from enum import Enum
>>> class Weekdays(Enum):
>>> MONDAY = 1
>>> TUESDAY = 2
>>> WEDNESDAY = 3
>>> THURSDAY = 4
>>> FRIDAY = 5
>>> SATURDAY = 6
>>> SUNDAY = 7
>>> day = Weekdays.MONDAY
>>> print("Today is", day.name)
>>> print("The corresponding value is", day.value)
In the above example, we define an enumeration 'Weekdays' with the days of the week as named constants. We can access the names and values of the constants using the name and value attributes, respectively.
Freezing Constants with frozenlist
The 'frozenlist' from the typing module can be used to create an immutable list of constants. This approach is helpful when you need to define a group of related constants. Here's an example:
>>> from typing import FrozenList
>>> FRUITS = FrozenList(["apple", "banana", "orange"])
>>> print("The available fruits are:", ", ".join(FRUITS))
In the code above, FRUITS is a frozen list of fruits. Since it is immutable, attempting to modify it will raise an error, ensuring that the list remains constant throughout the program.
Defining Constants in a Separate Module
Another common approach is to define constants in a separate module. By convention, this module is often named constants.py. Here's an example:
>>> # constants.py
>>> PI = 3.14159
>>> RADIUS = 10
>>> # main.py
>>> from constants import PI, RADIUS
>>> area = PI * (RADIUS ** 2)
>>> print("The area of the circle is:", area)
In this example, we define constants in the constants.py module and import them into the main.py file. This separation helps maintain a clean code structure and allows for easy modification of constants in a single location.
Module Takeaways…
Although Python doesn't have built-in support for constants, adhering to naming conventions and utilizing techniques like the enum module, frozenlist, or defining constants in a separate module can provide effective ways to achieve constant-like behavior.
By using these approaches, you can enhance code readability, reduce errors, and ensure the immutability of values that should remain constant throughout your programs.