Difference between Instance Variables and Class Variables in Python
In this article, we will learn about the types of variables offered by Python’s object model i.e. class variables and instance variables. OOPs allows for variables to be used as a class variable or as instance variable. But both have their own pros and cons.
Class Variables
Class variables are those variables that are defined within a class and before the class constructor i,e, __init__ method. Because class variables are shared by all instances of the class and owned by the class itself. Class variables are typically placed below the class name and outside of instance methods.
Let’s take an example:
class Person: person_name = "Albert" #Class Variable new_person = Person() print(new_person.person_name)
So in the above example, we declared a class Person and inside that class, we declared a variable. That variable is known as Class Variable.
OutPut:
Albert
This program returns the value of the variable. Class Variables can also contain any data type ( Like any other variable, ).
Instance Variables
Instance variables are declared inside the constructor method of the class i.e the __init__ method. Every object has its own value in an instance variable.
Let’s take an example:
class Person: def __init__(self, name, age): self.name = name #Instance Variable self.age = age #Instance Variable new_person = Person("Diptam", 20) print(new_person.name) print(new_person.age)
In this example, we created a Person class, and name and age are instance variables
OutPut:
Diptam 20
The output we receive is made up of the values of the variables that we initialized for the object instance of the new_person. as the Person class assigned the values to the instance variables. So it is clear that instance variables are owned by Objects of the class and each object can assign different values to those variables.
Differences Between Class Variable and Instance Variable in Python
Let’s again start with a short code:
class Bike: wheels = 2 # Class variable def __init__(self, name): self.name = name # Instance variable
The above code has a class name and one class variable and one instance variable. Since each bike model will have the same number of wheels, hence we declare that as a class variable. So now let’s call the class:
suzuki = Bike("Suzuki") bmw = Bike("BMW") print(suzuki.name, bmw.name)
Output:
Suzuki BMW
So accessing the instance variable name is pretty straightforward. However, accessing the class variable has a little more flexibility. We can call the class variable with the object name as well as with the class name:
print(suzuki.wheels) print(bmw.wheels) print(Bike.wheels)
Output:
2 2 2
But calling the instance variable with the class name (E.g. Bike.name) will through an attribute error since instance variables are object-oriented and are created when __init__ is invoked.
Now assume BMW has 1 wheel, in that case, if you modify the wheel of BMW, (e.g bmw.wheels = 1) it will change all bike’s wheel to 1. So changing or modifying a class variable affects all instances of the class. But this will not happen if we call wheels as an instance variable.
Also read:
Leave a Reply