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 Variable

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.

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.

output

We successfully get the value of the variable as the output. Now let’s add some more instances of the class to the program.

output

You can clearly see that all the instances have the same value because book_type is a class variable.

Just like 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 Variable

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.

Here 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.

output

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.

output

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.

output

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.

This Post Has 2 Comments

  1. Pinkesh Shah

    Great article on variables of Python !

    I have a question on class variables not being accessible to class methods. We can use instance variables within the method but not class variables.

    I will appreciate to know about its functionality in Python programming.

    Thank You

    1. Adarsh Kumar

      I think you are confused with class methods.

      I recommend you check out what actually are the class methods in Python?

      class methods are meant to access the class variables but not the instance variable.

      Normal function inside class has access to self parameter which can be used to access class as well as the instance variables but in class methods cls parameter is passed instead of self which has access to class variables only.

      Remember, class variable is accessible to any function unless it is not static and instance variable is accessible to only instance methods (normal methods inside a class with self parameter).

Leave a Reply