Python Class Variables vs Instance Variables

Posted in /   /  

Python Class Variables vs Instance Variables

Vinay Khatri
Last updated on June 10, 2022

    Python supports object-oriented programming which means we can use class and objects to create the programs. In the class, we can define two types of variables, class variables and instance variables. Both the variables are similar to the regular variables that we define in the function, and global scope, but accessing and using these two types of variables with objects can be tricky for a new Python learner. In this Python tutorial, we are going to discuss what is the difference between these two variables and see how do they work. We will also explore some examples that demonstrate the use of these variables and their functionalities. so let's get started with Class variables.

    What are Python class variables?

    The class variables are the variables that are defined in the root scope of the class. The value of the class variables is shared amongst all the objects of the class. The value of the class variables is created when we define the class itself.

    Example

    class Car:
        # class variable
        company = "Maruti Suzuki"
    
    # create the object of the class 
    raj_car = Car()
    
    # access car company
    print(raj_car.company)

    Output

    Maruti Suzuki
    In the above example, our class name is Car and it has only one class variable company . In this example, we have only defined one class variable but a class can have an arbitrary number of class variables.

    Accessing Class variable

    Now we know what is a class variable and how can we define it,  now let's discuss how can we access it in our program. As the name suggests class variable, the variable is more owned by the class than the objects of the class. Who can access the class variables? There are many ways to access class variables.
    1. Using the Class Name
    2. Using the object name
    3. using the self (within a class method)

    1. Access the Class Variable with Name

    We can access a class variable outside and inside the class using the class Name.
    Example
    class Car:
        # class variable
        company = "Maruti Suzuki"
    
    # access class variable with class name
    print(Car.company)

    Output

    Maruti Suzuki

    If we want to access the class variable inside the class method we can either use the object self or the class name. Example

    class Car:
        # class variable
        company = "Maruti Suzuki"
    
        def show_company(self):
            # access class variable with self
            print("The Company Name is:", self.company)
    
            # access class variable with class name
            # print("The Company Name is:", Car.company)
    
    # create the object of the class 
    raj_car = Car()
    
    raj_car.show_company()

    Output

    The Company Name is: Maruti Suzuki
    The Company Name is: Maruti Suzuki

    2. Access the Class Variable with object

    We can also use the object name to access a class variable outside the class. Example

    class Car:
        # class variable
        company = "Maruti Suzuki"
    
    # create the object of the class 
    raj_car = Car()
    
    # access class variable with object
    print(raj_car.company)

    Output

    Maruti Suzuki

    3. Access the Class Variable with self

    The class variable is present in the global scope for all the methods defined in the class. And if we wish to access a class variable inside any method there we have to take the help of the object self .  Accessing self.class_variable inside the method is similar to accessing object.class_variable outside the class.

    class Car:
        # class variable
        company = "Maruti Suzuki"
    
        def show_company(self):
            # accessing class variable with self 
            print(self.company)
    
    # create the object of the class 
    raj_car = Car()
    
    raj_car.show_company()

    Output

    Maruti Suzuki

    Changing Class variables

    There are only two ways to change class variables.

    1. Change with class Name
    2. Change with object name

    1. Change class variable with class name

    If we access the class variable with the class name and change its value then the changed value of the class variable will be reflected all over all the class objects. Example

    class Car:
        # class variable
        company = "Maruti Suzuki"
    
        def show_company(self):
            # accessing class variable with self 
            print(self.company)
    
    # create the object of the class 
    raj_car = Car()
    rahul_car = Car()
    
    # change class variable with class name 
    Car.company = "Suzuki"
    
    print("Raj Car Company:", end =" ")
    raj_car.show_company()
    print("Rahul Car Company:",end =" ")
    rahul_car.show_company()
    

    Output

    Raj Car Company: Suzuki
    Rahul Car Company: Suzuki

    In the above example, we changed the company value of the class from Maruti Suzuki to Suzuki with the class name, and its change for all the objects of the class raj_car and rahul_car .

    1. Change class variable with object name

    If we change the class variable with the object name or self, then the change will only reflect on that particular object. Example

    class Car:
        # class variable
        company = "Maruti Suzuki"
    
        def show_company(self):
            # accessing class variable with self 
            print(self.company)
    
    # create the object of the class 
    raj_car = Car()
    rahul_car = Car()
    
    # change class variable with object name 
    raj_car.company = "Suzuki"
    
    print("Raj Car Company:", end =" ")
    raj_car.show_company()
    print("Rahul Car Company:",end =" ")
    rahul_car.show_company()
    

    Output

    Raj Car Company: Suzuki
    Rahul Car Company: Maruti Suzuki

    In this example, we have changed the company variable using the object raj_car , that's why the change only reflects on the raj_car object.

    What is Python Instance Variables?

    The instance variables are more owned by the object itself, unlike the class variable they are not shared between two objects, and their value can be changed with every object creation. The instance variables are specified inside the class constructor and get automatically created for the class when the object of the class is created. Example

    class Car:
        # class variable
        company = "Tesla"
    
        def __init__(self, owner, model):
            # instance variables
            self.owner = owner
            self.model = model
        
        def show_detail(self):
            print("Owner: ", self.owner)
            print("Car Model: ", self.model)
    
    # create the object of the class 
    raj_car = Car('Raj', 'Y')
    
    raj_car.show_detail()
    

    Output

    Owner: Raj
    Car Model: Y

    In this example self.owner and self.model are the two instance variables and they only get created when we created its object raj_car .

    Accessing Instance variable

    To access the instance variables we use the object name followed by the dot operator and the instance variable name. Example

    class Car:
        # class variable
        company = "Tesla"
    
        def __init__(self, owner, model):
            # instance variables
            self.owner = owner
            self.model = model
    
    # create the object of the class 
    raj_car = Car('Raj', 'Y')
    
    # access instance variable with object
    print("Car Owner: ", raj_car.owner)
    print("Car Model: ", raj_car.model)
    

    Output

    Car Owner: Raj
    Car Model: Y

    Changing Instance variable

    The instance variables values are exclusive for individual instances or objects. If we change the one object's instance variables value it will not affect the other object's instance variables. Example Let's create two objects for a class and change the instance value for one of them.

    class Car:
        # class variable
        company = "Tesla"
    
        def __init__(self, owner, model):
            # instance variables
            self.owner = owner
            self.model = model
        
        def show_detail(self):
            print("Owner: ", self.owner)
            print("Model", self.model)
    
    # create the object of the class 
    raj_car = Car('Raj', 'Y')
    rahul_car = Car("Rahul", 'X')
    
    # change the instance variable for raj_car object
    raj_car.owner = "Raj Singh"
    
    raj_car.show_detail()
    rahul_car.show_detail()
    

    Output

    Owner: Raj Singh
    Model Y
    Owner: Rahul
    Model X

    Difference Between Python Class and Instance Variables

    Parameters Class Variables Instance Variables
    Declaration The class variables are declared within the root of the class definition. Instance variables are declared inside the Python constructor __init__() method.
    Owner Class variables are owned by the class. Instance variables are owned by the class instance or object.
    Creation Class variables get created when with the class. The Instance variables are created with the object creation.
    Access We can access a class variable with the class name and object name. An instance variable can only be accessed using an object or self.
    Example
    class class_name:
        class_variable = value
    class class_name:
        def __init__(self):
            self.instance_variable = value

    People are also reading:

    Leave a Comment on this Post

    0 Comments