Updated April 14, 2023
Introduction to Python Class Constants
An unchangeable value, assiged, is called a constant. Meaning, constants are the containers that hold some information and cannot be changed further. Write once, read many times constants are created in the class of Python. Usually, constants are defined in the level of a module. They are written in capital letters. An underscore separates each word. Though in reality, generally, the usage of class constants in Python isn’t done. Because the constants modules or global variables used are throughout the Python programs.
Example:
TOTAL, GROSS_BUDGET or assigning them like PI = 3.14
So, for the examples, creating a module file – constant.py is done.
Syntax:
As above mentioned, the syntax for Python class constants is something like this:
#Declaring a value to a constant
PI = 3.14
A value is declared and is known for being constant, is represented by giving capital letters to any variable considered.
Objects such as dictionaries and lists are mutable, that is, changeable. If const is bound to such objects, for example, a list object, the name is always bound to the object list. The contents of the list, though, may change. Meaning, unbound or rebound of items and more items can be removed and added with the object through methods like append.
How Does Python Class Constants Work?
In a human understanding, or non-technical, the constants can be considered as something you buy and don’t have an exchange policy. Or in other words, you buy some books, take any number of books, and taking the bag as a constant, those books once placed in the bag, cannot be replaced back again.
Now, getting into the actual working:
In Python, variables are named locations that are basically used to store data within the memory. The unique Identifier name is given to each and every variable. Variables do not need the declaration prior to reserving memory. Variable initialization automatically happens after assigning a value to a variable.
Example:
count = 0
employee_name = hemanth
age, agee, ageee = 22
But then, what if you want to create a global variable that is unchangeable and unique for itself?
For that, you need to declare a constant, which is pretty much the same as declaring a variable but with capital letters. Declaration of any variable with capitals considered a constant.
Working:
The Python constants are first created. These are identified to be constants as they are capitalized letters. They are given with an underscore in between to potrait it as a single variable. The variable is then assigned a value that is globally the same or consistent throughout the program. Once allocated, it is the same anytime.
Declaring a Constant in Python:
- constant.py is created earliest
GOLDEN_RATIO = 1.62
PI = 3.14
And then, in the main, you import the constant module.
- Value assigning to a constant in Python
main.py is created later:
import constant
print(constant.GOLDEN_RATIO)
print(constant.PI)
Examples of Python Class Constants
- Always declare a constant by capitalizing on the letters.
- Nomenclature is always a purpose.
- Python modules have constants put into Python modules and are meant not to be changed.
- Use underscores when you need them.
- Underscores makes the code pretty neat and readable. A word is neat and understandable than a letter. Let’s say you want to declare a variable number of case studies as a constant. You will realize that declaring it as CASE_STUDY is much more of a tip-off than saying it CS or C.
- You cannot start a constant variable with a digit.
- You cannot use special symbols like @, #, &, %, $ etc.
- You can always access the class constants from a superclass to a subclass.
Example #1
Code:
NAME = 'hemanth'
YOB = 1999
ID_NUM = 17783
print(NAME)
Output:
Example #2
Code:
NUM1 = 50
num2 = 65
print(num2+NUM1)
Output:
Accessing class constants from a subclass in a Python:
self.a_constant is used when you need to access any class constant that was defined previously in the superclass. An example will make a better understanding:
Example #3
Code:
#access class constants from superclass to subclass
class C1:
A_CONSTANT = 0.167355
class C2(C1):
def this_constant(self):
print(self.A_CONSTANT)
an_object = C2()
an_object.this_constant()
Output:
Conclusion
Other programming languages like C++ and Java does not provide this type of constants, unlike Python. Writing a variable in upper case letters, may it be any variable, is considered and termed a constant. String, Tuples, and Numbers are immutable. Binding a name with const to tuples, strings, and numbers, the name is always is bound to the object. And also, the content of the object’s will also always be the same because the object is immutable.
Recommended Articles
We hope that this EDUCBA information on “Python Class Constants” was beneficial to you. You can view EDUCBA’s recommended articles for more information.