Summary: In this tutorial, we will learn what class and instance variables are in Python and how they differ from each other in context to programming.
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"
book_type is a class variable assigned with “programming”.
Let’s create some instances of the class and output their
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)
We 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 (and shared) to each instance of the class.
Furthermore, since class variables are not bound to any instance, they can also be accessed via class name or reference.
class Book: book_type = "Sci-Fi" print(Book.book_type) #outputs Sci-Fi
We can define variables in the class’s scope by taking the advantage of the class reference (i.e.
self.__class__) in Python.
In this example, we have defined a class variable using
self.__class__ and later modified its value via
cls within the class method:
class Student: def __init__(self): #self.__class__ is the class reference self.__class__.school = "Pencil Programmer School" @classmethod def changeSchoolNameTo(cls, name): cls.school = name stud1 = Student() stud2 = Student() #changing the class variable school value via stud1 object stud1.changeSchoolNameTo("No Fee Programming School") #since class variable is shared among all the objects, #we can notice the change made by stud1 in the stud2 object. print(stud2.school) #outputs "No Fee Programming School"
Instance variables are the properties of objects in Python. This means that every object or instance of the class maintains a separate copy of the instance variable.
class Book: def __init__(self, bt): self.book_type = bt
book_type is an example of instance variable.
We can define instance variable in any instance method in Python. It is not mandatory to initialize the instance variable only in the
class Book: def __init__(self, bt): self.book_type = bt def setTitle(self, title): self.title = title book = Book("Programming") book.setTitle("Go Hero in Python") print(book.title) #outputs "Go Hero in Python" print(book.book_type) #outputs "Programming"
In this example, we have defined two instance variables in two different instance methods.
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)
You can see that each instance of the same class has different values of
book_type property, which was passed during the creation of instances.
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 Books of the same publication but of different genre.
Since their publication are same but the type is different, we have defined
publication as the class variable and
book_type as the instance variable.
This means that the value of
publication will be shared (same for both instances) and the
book_type will be different for both.
When to use class or instance variable?
We should use class variable when we want to have same value for all the instances (objects), otherwise use an instance variable.