Difference b/w class variable and instance variable Python

Class Variables and Instance Variables in Python

In Python, there are two types of variables: class variables and instance variables. Understanding the difference between these two types of variables is crucial for writing efficient and organized code. This is bit different from other languages.

Class Variables

Class variables, also known as static variables, are variables that are shared by all instances of a class. They are defined outside of any method or constructor and are associated with the class itself, not with any particular instance.

To declare a class variable, you simply assign a value to it inside the class definition, using the = operator. Here’s an example:

class MyClass: class_variable = "This is a class variable" print(MyClass.class_variable) # Output: This is a class variable

obj1 = MyClass()
obj2 = MyClass()
print(obj1.class_variable) # Output: This is a class variable
print(obj2.class_variable) # Output: This is a class variable
Enter fullscreen mode Exit fullscreen mode

Note that class variables are shared by all instances of the class. If you modify the value of a class variable in one instance, it will affect all other instances as well.

Instance Variables

Instance variables, on the other hand, are variables that are unique to each instance of a class. They are created when an instance of the class is created and they are associated with that specific instance.

To declare an instance variable, you need to define it inside a method or constructor of the class. Here’s an example:

class MyClass: def __init__(self, instance_variable): self.instance_variable = instance_variable obj1 = MyClass("obj1 instance variable")
print(obj1.instance_variable) # Output: obj1 instance variable
 obj2 = MyClass("obj2 instance variable")
print(obj2.instance_variable) # Output: obj2 instance variable
Enter fullscreen mode Exit fullscreen mode

In this example, instance_variable is an instance variable of the MyClass class. It is initialized in the __init__ (constructor) method and can be accessed using the self keyword.

Note that each instance of the class has its own copy of the instance variable. Modifying the value of an instance variable in one instance will not affect the value in other instances.

Differences and Uses

The main difference between class variables and instance variables is that class variables are shared by all instances of the class, while instance variables are unique to each instance.

Class variables are useful when you want to share data between all instances of a class. For example, you might use a class variable to keep track of the number of instances created, or to store a constant value that is used by all instances.

Instance variables, on the other hand, are useful when you want to store data that is specific to each instance. For example, you might use an instance variable to store the name of a person, or the age of a person.


I hope this article helps you understand class variables and instance variables in Python. Let me know if you have any further questions!


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Martins ad network.