Variable Types in Python, Local, Instance, Static

A variable is an entity that continuously 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 data type it will accept, and sometimes it is automatically initialized, the This factor depends on the programming language.

But in this article, our main goal is to understand variable types in python programming language.

In addition, we will focus only on the types of variables used inside the say class, object or method.

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

  1. Version variable
  2. Local variable
  3. Static variable

Here we will go deeper to understand all 3 of these categories and believe me it is the smallest and easiest concept that you do not want to ignore in vain.

Let’s first understand the hard part first then moderate and then I’ll explain in a way that makes the concept clear.

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

The scope of the instance variable is inside the constructor. Also, the data inside the instance variable varies from object to object. This is the tricky part and yes it does seem confusing.

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

In this case, the variables inside the constructor containing the data are called instance variables.

Version variable in python

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

Variables that are combined with self keyword and go inside any method are called local variables, 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 actual coding.

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

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

To pass down instance variable values ​​you need another variable to hold that data and pass it safely inside any method.

So, the task of holding this instance variable’s values ​​is done by a variable associated with the self keyword.

local variable in python

In the image above, you can see our ‘name’ and ‘age’ storage data comes from object s1.

In lines 3 and 4, we have passed the values ​​of instance variable to another variable named ‘Name’ and ‘Age’, these two variables are also associated with self-keyword and these two variables themselves will be in show() method. So these two variables ‘Name’ and ‘Age’ are called local variable.

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

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

Static variable in python

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

We have a local variable ‘Name’ and ‘Age’. We have defined one more variable that has nothing to do with the object, which is also defined outside a method, constructor and inside a class.

This variable is ‘Field’, this variable is called static variable.

The reason why it’s static is that it doesn’t change from object to object and remains constant throughout.

For your better understanding, let’s look at all three types of variables i.e. instance variable, local variable and static variable at the same time.

variable types in python


News7F: Update the world's latest breaking news online of the day, breaking news, politics, society today, international mainstream news .Updated news 24/7: Entertainment, the World everyday world. Hot news, images, video clips that are updated quickly and reliably

Related Articles

Back to top button