Create Constant Variable in Python using Pconst Library
Constant variable the name itself says that it is constant. We have to define a constant variable at the time of declaration. After that, we will not able to change the value of a constant variable. In some cases, constant variables are very useful.
Creating constant variables, functions, objects is allowed in languages like c++, Java. But in python creating constant variable, it is not allowed. There is no predefined type for a constant variable in Python. But we can use pconst library for that.
pip install pconst
Below are some examples which depict how to use constants in python
You can set constants to const module’s attribute.
If try to update the constant value, ConstantError will be raised.
The del operator is disallowed.
You can also set dict and list value to const module, and they will be not editable (if dict or list values contains dict or list, then will be applied recursively.).
Please Login to comment...