37.instance & Class Variables Python
37.instance & Class Variables Python
Hindi #54
Instance variable
Class variable
Instance variable:
We can also say that the value is different for every object
that we create. Let us dive into some in-depth
explanation. When we create a class, we define a few
variables along with it. For example, we have created a
class of Students, and we have defined a variable age. All
the students cannot have the same age in a class, so we
have assigned the variable an average age of 16. Now,
whenever we use an object to print the value of age, it will
show 16. We can try to change the value of age, but it will
create a new instance variable for the specific object that
we are updating it for, hence defining the value to it.
Std1.age = 18
Class variable:
A quick review:
harry = Employee()
rohan = Employee()
harry.name = "Harry"
harry.salary = 455
harry.role = "Instructor"
rohan.name = "Rohan"
rohan.salary = 4554
rohan.role = "Student"
print(Employee.no_of_leaves)
print(Employee.__dict__)
Employee.no_of_leaves = 9
print(Employee.__dict__)
print(Employee.no_of_leaves)