Types of variables in python | Instance variable in python | Local variable in python | Static variable in python

A variable is an entity that keeps on changing according to its behavior.

We use different types of variables throughout any programming language, sometimes we have to initialize those variables based on what type of data it is going to accept, and sometimes it is auto initialized, these factors are dependent on the programming language.

But in this article, our main goal is to understand the types of variables in a python programming language.

Also, we will be focused only on types of variables used inside the class, object, or say method.

There are 3 types of variables in a python programming language.

  1. Instance variable
  2. Local variable
  3. Static variable

Here we will go in bone-deep to understand all these 3 types and trust me it is the smallest and easiest concept which you don’t want to let go in vain.

  • Instance variable

Let’s first understand the tough part first then moderate and then I will explain in a way that will make this concept crystal clear.

It is a variable that is associated with a particular object, and it is used to hold data coming from the object directly.

The scope of the instance variable is inside the constructor. Also, data inside instance variable changes from object to object. This was the tough part and yes it might seem confusing.

 When you create a constructor inside the class, there are variables inside those constructors which is used to hold data coming directly from the object of a class.

In this situation, the variables inside the constructor which holds data are called instance variables.

Instance variable in python

In the above code on line 2, we have initialized constructor and inside that constructor, we have two variables named ‘name’ and ‘age’ which are used to hold data coming from object s1. These two variables ‘name’ and ‘age’ is called instance variable.

  • Local variable

The variable which is associated with self-keyword and goes inside any method is called a local variable, also the scope of these variables is only inside the method.

You can consider the above statement as a definition of a local variable. Now let us understand this definition with actual coding. 

You have created a class and assigned some values to a class using an object (s1), now these values come into the constructor and the instance variable will hold these values.

From here on you want to pass those values inside a method in this case it is shown () method. If you don’t know what is method read it here quickly.

For passing down values of instance variable you need another variable to hold that data and transfer it safely inside any method.

So, this task of holding values of the instance variable is done by a variable that is associated with self-keyword.  

local variable in python

In the above picture, you can see our variable ‘name’ and ‘age’ hold data coming from object s1.

On lines 3 and 4, we have transferred values of the instance variable to another variable named ‘Name’ and ‘Age’, these two variables are also associated with self-keyword and it is these two variables that are going inside method show(). So, these two variables ‘Name’ and ‘Age’ is called local variable.

  • Static variable

If the value of the variable does not change from object to object and if the variable is declared outside method and inside the class and also the value of variable remains same throughout this type of variable is called as static variable.

You can consider this as a definition. Also, it is the easiest concept.

Static variable in python

In the above example, we have instance variables as ‘name’ and ‘age’.

We have a local variable as ‘Name’ and ‘Age’. We have defined one more variable that does not have anything to do with the object, also defined outside a method, constructor, and inside a class.

This variable is ‘School’, it is this variable which is called a static variable.

The reason why it is static is that it does not change from object to object and remains constant throughout. 

For your better understanding let us see all of the three types of variables that is instance variable, local variable, and static variable all at once.

types of variable in python

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published.