Как использовать константы в Python
Python Constants: Improve Your Code’s Maintainability
In programming, the term constant refers to names representing values that don’t change during a program’s execution. Constants are a fundamental concept in programming, and Python developers use them in many cases. However, Python doesn’t have a dedicated syntax for defining constants. In practice, Python constants are just variables that never change.
To prevent programmers from reassigning a name that’s supposed to hold a constant, the Python community has adopted a naming convention: use uppercase letters. For every Pythonista, it’s essential to know what constants are, as well as why and when to use them.
Understanding Constants and Variables
Variables and constants are two historical and fundamental concepts in computer programming. Most programming languages use these concepts to manipulate data and work in an effective and logical fashion.
Variables and constants will probably be present in each project, app, library, or other piece of code that you’ll ever write.
What Variables Are
Variables are names that hold values assigned to them during the program’s execution. These values can be changed during runtime, which means that the variable can be reassigned to a different value.
In Python, variables are dynamically typed, which means that you don’t need to explicitly declare their types. The type of a variable is inferred from the value assigned to it.
What Constants Are
Constants, on the other hand, are names that represent values that remain unchanged throughout the program’s execution. Once a constant is assigned a value, it cannot be modified or reassigned. Constants are used to store values that are known and fixed.
In Python, there is no specific syntax for constants. You can define constants by using uppercase letters to emphasize that the name represents a value that should not be changed.
Why Use Constants
Constants provide several benefits in code development:
-
Readability: By using constants, you give meaningful names to values, making your code more readable and self-explanatory.
-
Maintainability: Constants make your code easier to maintain. When you need to change a value that is used in multiple places, you only need to update the constant’s value in one place.
-
Reusability: Constants can be used in different parts of your codebase, allowing you to reuse a specific value without duplicating its definition.
When to Use Constants
You should use constants when a value in your code is known and remains fixed. Here are some common use cases for constants:
-
Mathematical constants: Constants like pi (π), Euler’s number (e), or the golden ratio (φ) are known and used in mathematical calculations.
-
Configuration settings: Values like API keys, file paths, or database connection strings are usually fixed and should be defined as constants.
-
Magic numbers: Instead of using arbitrary numbers directly in your code, you can define constants with meaningful names. This improves the code’s readability and makes it easier to understand the purpose of numeric values.
Defining Your Own Constants in Python
Python doesn’t have a dedicated syntax for defining constants. Instead, you can define constants by using uppercase letters to represent that the variable should not be reassigned. Let’s explore two common approaches to defining constants in Python.
User-Defined Constants
In Python, you can define user-defined constants by assigning a value to a variable with an uppercase name. For example:
By convention, constant names in Python are written in uppercase letters.
Module-Level Dunder Constants
Another approach to defining constants is by using module-level dunder (double underscore) names. These names are reserved for special use and should not be modified. By convention, Python uses uppercase letters for these names to indicate that they are constants.
For example, you can define constants in a module called config.py
as follows:
You can then import these constants into other modules using the import
statement:
Putting Constants Into Action
Now that you know how to define constants, let’s see how to put them into action to improve your code’s maintainability, readability, and reusability.
Replacing Magic Numbers for Readability
Consider the following code snippet:
In this code, the value 3.14159 is a magic number. Magic numbers are arbitrary values that are hard to understand and may lead to confusion. To improve the code’s readability, you can define a constant for pi and use it as follows:
By using a constant for pi, you make the code more readable and self-explanatory.
Reusing Objects for Maintainability
Constants can also be used to store and reuse objects, such as database connections, API clients, or configuration settings. By reusing objects, you reduce redundant code and improve the maintainability of your codebase.
For example, instead of creating a new database connection object every time you need to interact with the database, you can define a constant for the database connection and reuse it throughout your code:
By defining the database connection as a constant, you only need to establish the connection once, improving performance and maintainability.
Providing Default Argument Values
Constants can also be used as default argument values in function definitions. This allows you to provide a default value that can be overridden when necessary.
For example, consider a function that calculates the area of a rectangle:
By defining default argument values using constants, you can provide sensible defaults while allowing flexibility:
This way, the function can be called with or without arguments, and the default values will be used when not provided.
Handling Your Constants in a Real-World Project
When working on a real-world project, handling constants effectively is crucial for code organization and maintainability. Let’s explore some common approaches for managing constants in Python projects.
Putting Constants Together With Related Code
One way to manage constants is to put them together with the related code that uses them. For example, if you have constants for configuration settings, you can define them in the same module or class where the configuration code resides.
By keeping the constants in the same class as the configuration code, you ensure that they are easily accessible and maintainable.
Creating a Dedicated Module for Constants
Another common approach is to create a dedicated module specifically for constants. This module can be imported wherever the constants are needed.
This approach keeps the constants separate from the code logic, making it easier to manage and update them.
Storing Constants in Configuration Files
For larger projects, it can be beneficial to store constants in configuration files. This allows you to manage different sets of constants based on different environments or configurations.
For example, you can use a YAML or JSON file to store your constants:
By storing constants in configuration files, you can easily switch between different environments and configurations without modifying the code.
Handling Constants as Environment Variables
Another approach to handle constants is to store them as environment variables. Environment variables are system-wide values that can be accessed by any process running on the system.
To access the values of environment variables in Python, you can use the os
module:
By using environment variables, you can store sensitive values like API keys or database connection strings outside of your codebase. This improves security and allows for easy configuration across different environments.
Exploring Other Constants in Python
In addition to user-defined constants, Python provides several built-in constants and internal dunder names that you can use in your code.
Built-in Constants
Python includes several built-in constants that are commonly used in mathematical calculations. These constants are defined in the math
module:
Internal Dunder Names
Python uses internal dunder names with a specific structure to indicate that they should be treated as constants. These names are found in various modules and can be accessed as needed. For example:
Useful String and Math Constants
The string
module includes some constants that are useful when working with strings, such as:
The math
module also provides some useful constants, such as:
Type-Annotating Constants
When defining constants, you can use type annotations to provide additional information about the constant’s type. Type annotations help improve the clarity and readability of your code, especially when working in a team or maintaining a large codebase.
For example, you can annotate a constant as an integer:
By adding type annotations to your constants, you make it clear what type of value the constant represents.
Defining Strict Constants in Python
While Python doesn’t provide a built-in mechanism for defining strict constants, there are several approaches you can use to make constants strictly constant and prevent accidental changes.
The .slots Attribute
By using the __slots__
attribute, you can define a list of allowed attributes for a class. This restricts the creation of new attributes, effectively making the existing ones constant.
The @property Decorator
The @property
decorator allows you to define read-only attributes in a class. These attributes can be accessed like normal attributes but cannot be modified.
The namedtuple() Factory Function
The namedtuple()
factory function from the collections
module can be used to create immutable objects with named fields. These objects act as strict constants because once created, their attribute values cannot be changed.
The @dataclass Decorator
The @dataclass
decorator from the dataclasses
module is used to create data classes with additional functionality, including immutability. By specifying frozen=True
as a parameter in the dataclass
decorator, the resulting objects become immutable.
The .setattr() Special Method
By overriding the __setattr__()
special method, you can prevent attribute assignment and effectively make the attributes constant.
By using these techniques, you can enforce the constant behavior of specific attributes in your classes.
Conclusion
In this tutorial, you learned how to define constants in Python and how to use them to improve your code’s maintainability, readability, and reusability. You explored different approaches for organizing and managing constants in a project, such as putting them together with related code, creating a dedicated module, storing them in configuration files, and handling them as environment variables. You also discovered built-in constants, internal dunder names, and useful string and math constants provided by Python. Finally, you explored techniques for making constants strictly constant in Python using various methods like __slots__
, @property
, namedtuple()
, @dataclass
, and __setattr__()
. By using constants effectively, you can write clean, maintainable, and resilient code in Python.
To learn more about how to use constants in Python, you can download the sample code provided in this tutorial.