A variable is an entity that changes according to its behavior.
We use different types of variables in any programming language, sometimes we have to initialize those variables based on the type of data it will accept and sometimes it is automatically initialized, these factors depend 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’ll focus only on the types of variables used within the class, object, or say method.
There are 3 types of variables in a Python programming language.
- variable instance
- local variable
- static variable
Here we will dig deeper to understand these 3 types and believe me, it is the smallest and easiest concept that you do not want to pass up in vain.
First let’s understand the hard part, then let’s moderate, and then I’ll explain it in a way that will make this concept very clear.
It is a variable that is associated with a particular object and is used to store data that comes directly from the object.
The scope of the instance variable is inside the constructor. Also, the data inside the instance variable changes from one object to another. This was the hard part and yes, it can seem confusing.
When you create a constructor inside the class, there are variables inside those constructors that are used to hold data that comes directly from the object of a class.
In this situation, the variables inside the constructor that hold data are called instance variables.
In the above code on line 2, we have initialized the constructor and within that constructor, we have two variables called ‘name’ and ‘age’ which are used to store data coming from object s1. These two variables ‘name’ and ‘age’ are called Variable instance.
The variable which is associated with the keyword itself and goes inside any method is called local variable, also the scope of these variables is only inside the method.
You can think of the above statement as a definition of a local variable. Now let’s understand this definition with the actual encoding.
You have created a class and assigned some values to a class using an object (s1), now these values go into the constructor and the instance variable will hold these values.
From here, you want to pass those values inside a method; in this case, the method() is displayed. If you don’t know what the method is, read it here quickly.
To pass instance variable values, you need another variable to store that data and pass it safely inside any method.
Therefore, this task of holding instance variable values is performed by a variable associated with the keyword itself.
In the image above, you can see that our ‘name’ and ‘age’ variables contain data from the s1 object.
In lines 3 and 4, we have passed values from the instance variable to another variable called ‘Name’ and ‘Age’, these two variables are also associated with the proper keyword and it is these two variables that go inside the show() method. So these two variables ‘Name’ and ‘Age’ are called local variable.
If the value of the variable does not change from one object to another and if the variable is declared outside the method and inside the class and also the value of the variable remains the same throughout this type of variable, it is called a static variable.
You can consider this as a definition. Plus, it’s the easiest concept.
In the example above, we have instance variables like ‘name’ and ‘age’.
We have a local variable like ‘Name’ and ‘Age’. We have defined one more variable that has nothing to do with the object, also defined outside of a method, constructor and inside of a class.
This variable is ‘School’, it is this variable that is called static variable.
The reason why it is static is that it does not change from one object to another and remains constant at all times.
For your better understanding, let’s look at the three types of variables which are instance variables, local variables, and static variables all at once.