In this post, we will understand the concept of class and instance variables in object-oriented programming within Python.
Python being an object-oriented programming language allows variables to be used at class level or at the instance level.
When the variables are declared at the class level they are referred to as class variables whereas when they are declared at instance level they are referred to as instance variables.
We declare variables at class level when we expect all the objects (instances of a class) to share a single copy of a variable, whereas we declare variables at instance level when each object needs to have a separate copy of the variable so that they can have different values stored in them.
Let’s understand with examples.
Recommended: object oriented programming in Python
Class variables are shared by all the instances of the class (objects) because they are owned by the class. If it gets changed by any single object it changes for all objects.
Class variables are declared within class outside of any methods, usually just below the class header.
class Book: book_type = "programming"
Here book_type is a class variable assigned with “programming”.
Let’s create an instance of the class and print the value of class variable.
class Book: book_type = "programming" python_book = Book() print(python_book.book_type)
We successfully get the value of the variable as the output. Now let’s add some more instances of the class to the program.
class Book: book_type = "programming" python_book = Book() java_book = Book() c_book = Book() print(python_book.book_type) print(java_book.book_type) print(c_book.book_type)
programming programming programming
You can clearly see that all the instances have the same value because
book_type is a class variable.
book_type, we can declare multiple class variables inside of the class and each of them will be accessible to each instance of the class.
Instance variables are the properties of objects. This means that every object or instance of the class maintains a separate copy of the variable.
Instance variables are declared within the method.
class Book: def __init__(self, bt): self.book_type = bt
book_type is an example of instance variable. When we create an object of the class we need to define the values of instance variable hence we have to pass the values for them during object declaration.
Let’s see an example.
class Book: def __init__(self, bt): self.book_type = bt python = Book("programming") print(python.book_type)
We have successfully got the output of the instance variable as what we have assigned at object declaration.
Now let’s create some more instances of the class.
class Book: def __init__(self, bt): self.book_type = bt python = Book("programming") Recepie = Book("Cooking") Maharanapratap = Book("Biography") print(python.book_type) print(Recepie.book_type) print(Maharanapratap.book_type)
programming Cooking Biography
You can see that each instance has a different value for its book_type property, which is passed during the declaration.
Hence it is proved that the instance variables are owned by the objects.
Using Class and Instance variable Together
Now as we have some idea of class and instance variable let’s see how they can be used together.
class Book: publication = "Pencil_Programmer_Publication" def __init__(self, bt): self.book_type = bt python = Book("programming") Maharanapratap = Book("Biography") print("Python: "+python.book_type+" - "+python.publication) print("Maharanapratap: "+Maharanapratap.book_type+" - "+Maharanapratap.publication)
Python: programming - Pencil_Programmer_Publication Maharanapratap: Biography - Pencil_Programmer_Publication
In the above example, we have created two ‘Book’ objects of the same publication but of different categories. Since their publication are same but the type is different therefore we have defined
publication as a class variable and
book_type as instance variable so that it can hold different value for different instances of the class.
In conclusion, use a class variable when you want to have same value for all objects otherwise use an instance variable.
Hope you have understood the concept of class and instance variables. If you have any doubts or suggestions then please comment below.