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 orga…


This content originally appeared on DEV Community and was authored by Ankit malik

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

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

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!


This content originally appeared on DEV Community and was authored by Ankit malik


Print Share Comment Cite Upload Translate Updates
APA

Ankit malik | Sciencx (2024-08-06T20:10:03+00:00) Difference b/w class variable and instance variable Python. Retrieved from https://www.scien.cx/2024/08/06/difference-b-w-class-variable-and-instance-variable-python/

MLA
" » Difference b/w class variable and instance variable Python." Ankit malik | Sciencx - Tuesday August 6, 2024, https://www.scien.cx/2024/08/06/difference-b-w-class-variable-and-instance-variable-python/
HARVARD
Ankit malik | Sciencx Tuesday August 6, 2024 » Difference b/w class variable and instance variable Python., viewed ,<https://www.scien.cx/2024/08/06/difference-b-w-class-variable-and-instance-variable-python/>
VANCOUVER
Ankit malik | Sciencx - » Difference b/w class variable and instance variable Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/06/difference-b-w-class-variable-and-instance-variable-python/
CHICAGO
" » Difference b/w class variable and instance variable Python." Ankit malik | Sciencx - Accessed . https://www.scien.cx/2024/08/06/difference-b-w-class-variable-and-instance-variable-python/
IEEE
" » Difference b/w class variable and instance variable Python." Ankit malik | Sciencx [Online]. Available: https://www.scien.cx/2024/08/06/difference-b-w-class-variable-and-instance-variable-python/. [Accessed: ]
rf:citation
» Difference b/w class variable and instance variable Python | Ankit malik | Sciencx | https://www.scien.cx/2024/08/06/difference-b-w-class-variable-and-instance-variable-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.