In the last article we learnt how to create a class and its instance. Today we would look at the difference between an instance variable and a class variable.
Instance variables are variables used for data that is unique to a particular instance. Example of instance variables are the ones we used in the previous article.
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' +last+ '@company.com'
All of this are initialized for each of the unique instances that we would create, this instances in this context are the employees we would create.
Whereas,
Class Variables are variables that are shared by all instances of a class. So while instance variable can be unique for each instance like our name,email and pay; class variable should be the-same for each instance.
Now looking at our (Employee) class in the previous example any data that has to be shared by all instances of the Employee class has to be stored in a class variable so all the instances created can have access to it. An example of such data could be a yearly raise for each employee.
To do this, let's say we create a new method named (apply_raise) and instead of using a class variable we use an instance variable;
class Employee:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def apply_raise(self):
self.pay = int(self.pay * 1.04)
emp_1 = Employee('John','Son',60000)
print (emp_1.pay)
emp_1.apply_raise()
print(emp_1.pay)
Running this program would give us our desired output; but what if we have a situation where we need to output the raise amount or even update the raise amount. Our (BIG) brains would trickily as us to change the (1.04) rate on the (apply_raise) method, but since we are learning OOP that wouldn't be a nice approach.
We would need to create a class variable to contain the data about the pay raise.
class Employee:
raise_amount = 1.04
Now on our (apply_raise) method we would have;
def apply_raise(self):
self.pay = int(self.pay * self.raise_amount)
We were able to pass in the (raise_amount) because it's a class variable and can be accessed by all instances within the parent class.
Now if we print the raise amount;
print(Employee.raise_amount)
print(emp_1.raise_amount)
print(emp_2.raise_amount)
We get the-same output, which proves that class variables could be accessed by your class and at the-same time instances of the class.
So what happens here is that when we try to access an attribute on an instance, it first checks whether the instance contains the attribute; if it doesn't, it checks if the parent class or any class it inherits from contains the attributes; so when we access (raise_amount) from our instances, they don't actually have that attribute but rather they are accessing the classes the (raise_amount) variable attributes.
Another place we could use a (class variable); say we are expected to output the number of employees. Our entire code would now look like this:
class Employee:
no_emps = 0
raise_amount = 1.04
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' +last+ '@company.com'
Employee.no_emps +=1
def fullname(self):
return self.first + ' ' + self.last
def apply_raise(self):
self.pay = int(self.pay * self.raise_amount )
print(Employee.no_emps)
emp_1 = Employee('John','Son',50000)
emp_2 = Employee('Jane','Son',80000)
print(Employee.no_emps)
print (emp_1.fullname())
print (emp_1.email)
print (emp_1.pay)
print (emp_2.pay)
emp_1.apply_raise()
emp_2.apply_raise()
print (emp_1.pay)
print (emp_2.pay)
Next, we would be looking at distinguishing regular, static and instance methods in python. Please ask as much questions you can, so we can all learn together. Also leave your contribution on the comment box to improve the piece for all. Like and share.
Top comments (10)
hello dear Johnson,
many thanks for the great introduction into instance variables and class variables.
this is extremely helpful and very very convincing tutorial and training.
one question though: would you do some tutorials in the same way - discussing the
corey materials in depth ...
eg the following:
Python OOP 2 - Class Variables - youtu.be/BJ-VvGyQxho
[you allready did it : Python OOP 3 - Classmethods and Staticmethods - youtu.be/rq8cL2XMM5M ]
Python OOP 4 - Inheritance - youtu.be/RSl87lqOXDE
Python OOP 5 - Special (Magic/Dunder) Methods - youtu.be/3ohzBxoFHAY
Python OOP 6 - Property Decorators - youtu.be/jCzT9XFZ5bw
that would be fantastic. Love to hear from you
greetings
Hello Linoman, i'm sorry i am replying a year after and you have probably gotten to understand those concepts, but thank you. I would try to write an article on those.
Thank you for doing these little tutorials! I'm just now learning python so I find these extremely helpful.
You re always welcome. I'm happy you find them helpful
So in this case you have used class variable to store a constant however is there any other use case where we can use class variable instead of instance variable?
So whenever you need to create a variable that needs to be shared by all instances or methods in a class, then and only then it makes sense to use class variables
this example is taken from Correy Schafer
Found it to be easier, to teach the concept discussed
Thank you Ogwuru for this article.
In the example, all the Instance Variables have self. appended to them whereas Class Variables don't. Is it like this or is it a coincidence?
Its not a coincidence