In this Python oops program, we will create a Python class with class variables. The program demonstrates the usage of a class variable in Python.
Python class with class variables
- The code demonstrates the usage of a Python class with class variables.
- A class variable is a variable that is shared by all instances of a class. It is defined within the class but outside any methods.
- In this case, the class_var class variable is assigned the value “Hello” within the MyClass class definition.
- By accessing MyClass.class_var, we can directly retrieve and print the value of the class_var class variable, which will output “Hello” in this case.
- Class variables are useful when you want to define attributes that are common to all instances of a class.
- They are shared among all instances and can be accessed without creating an object of the class.
Output:
Related Articles
Python oops program to create a class with a static method.
Python oops program to create a class with the class method.
Write a Python Class to get the class name and module name.
Write a Python Class object under syntax if __name__ == ‘__main__’.
Python class with Single Inheritance.
Python Class with Multiple Inheritance.