Class Variables in Python
In this lesson, you will learn about what is attributes or class variables in python and how to use them in a class in python.
Class Variables in Python
Any variable declared inside a class before any methods or functions are called a Class variable or static variable. Other than that, all other variables are called instance variables.
Let’s understand more about class variables in python using an example
Sample program using the class variable in python
Input
[gamipress_button label=”Try It” onclick=”window.open(‘https://coderseditor.com/?id=1415′,’_blank’)”]
class Class_variable: gender = 'male' def __init__(self,name,age): self.name = name self.age = age a = Class_variable('Nathan', 27) b = Class_variable('William', 37) print(a.name) print(a.gender) print(a.age) print(b.name) print(b.gender) print(b.age)
Output:
Nathan male 27 William male 37