How do I create a constant in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, the concept of a "constant" isn't formally defined or enforced by the language as it is in some other programming languages like C++ or Java. However, Python developers use conventions to indicate that a variable is intended to be immutable and should not be changed. In this article, we'll explore how to create and use constants in Python, including technical explanations and examples.
Why Use Constants?
Using constants in your code can greatly improve readability and maintainability. It signifies to other developers or your future self that a particular value should remain unchanged throughout the program's lifecycle. This can prevent accidental value changes and help in understanding the code's logic.
Defining Constants in Python
While Python lacks a built-in constant type, the community follows certain conventions to implement the concept of constants:
- Naming Conventions: Use uppercase letters with words separated by underscores to declare a constant. This naming style is a widely respected convention in Python to denote an immutable object.
- Modules: Since everything in a module is considered as part of the module's namespace, constants are often placed in a separate module file.
Import these constants into other Python scripts:
Enforcing Constants
Although Python does not enforce immutability, you can use some workarounds to enforce constant behavior:
Using Custom Classes
Create a class with read-only properties.
Attempting to change the value will lead to an AttributeError.
Using the enum Module
You can use the enum module to define constants.
Pitfalls and Caveats
- Variable Mutability: If a constant is a mutable object such as a list or dictionary, the object itself can still be modified even if the variable is intended to be constant:
- Enforcement: The enforcement of constant behavior should be done through discipline and adherence to conventions, as Python doesn't enforce constant values.
Summary Table
| Feature | Description |
| Syntax Convention | Use uppercase letters |
| Module Usage | Place constants in a separate module |
| Enforced Immutable | Use custom classes or enum module |
| Mutable Constants | Mutable types can have their contents modified |
| Enforcing Changes | Python does not enforce immutability |
Conclusion
Python may not have a built-in mechanism for defining constants, but through community conventions and creative use of its flexibility, you can effectively employ constants in your programming projects. By maintaining good coding practices and using Python's capabilities strategically—such as through naming conventions, modules, and classes—you can create clean, maintainable, and reliable code. While Python relies on programmer discipline rather than enforced rules, this practice promotes better code quality and program stability.
Related reading
- How do I create a datetime in Python from milliseconds?
- How do I create a list with numbers between two values?
- How do I create a multiline Python string with inline variables?
- How do I create a new column where the values are selected based on an existing column?
- How do I create a new database in MongoDB using PyMongo?
- How do I create a numpy array of all True or all False?
- How do I create a second new plot, then later plot on the old one?
- How do I create a slug in Django?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.