MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Instance & Class Variables


Instance variable:


"Instance variables are the variables for which the value of the variable is different for every instance."

We can also say that the value is different for every object that we create. Let us dive into some in-depth explanations. When we create a class, we define a few variables along with it. For example, we have created a class of Students, and we have defined a variable age. All the students cannot have the same age in a class, so we have assigned the variable an average age of 16. Now, whenever we use an object to print the value of age, it will show 16. We can change the value of age, but it will create a new instance variable for the specific object that we are updating it for, hence defining the value to it.

The code for changing age for a particular object will be something like this:

Std1.age = 18


Class variable:


"Class attributes are owned by the class directly, which means that they are not tied to any object or instance."

Same as in the above example, if we want to change the age for every instance from 16 to 17, then we can do it by using the class variable, which in this case is Student.

"It is worth noting that updating the value of the class variable will not change it for the instance variables of the objects, such as in the case above."

The code for changing age using a class variable will be something like this:

Students.age = 18


The following are the notable differences between Class (static) and instance variables.

Instance variables

When an object is created with the use of the new keyword, instance variables are created. They are destroyed when the object is destroyed.
Instance variables can be accessed by calling the variable name inside the class. ObjectReference.VariableName.
Every instance of the class has its own copy of that variable. Changes made to the variable don't affect the other instances of that class.


Class variables

When the program starts, static variables are created and destroyed when the program stops.
Static variables can be accessed by calling using a class name. ClassName.VariableName.
There is only one copy of that variable that is shared with all instances of the class. If changes are made to that variable, all other instances will be affected.


The __dict__ attribute


Every object in Python has an attribute that is denoted by __dict__. It maps the attribute name to its value. This dictionary stores all the attributes defined for the object itself. Following is the syntax of using __dict__:

object.__dict__

Example


class Employee:
    no_of_leaves = 8
    pass

harry = Employee()
rohan = Employee()

harry.name = "Harry"
harry.salary = 455
harry.role = "Instructor"

rohan.name = "Rohan"
rohan.salary = 4554
rohan.role = "Student"

print(Employee.no_of_leaves)
print(Employee.__dict__)
Employee.no_of_leaves = 9
print(Employee.__dict__)
print(Employee.no_of_leaves)



Conclusion

In this page (written and validated by ) you learned about Python Instance and Class Variables . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Self and init Constructors.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.