Python | Avoiding class data shared among the instances Last Updated : 15 Jul, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Class attributes belong to the class itself and they will be shared by all the instances and hence contains same value of each instance. Such attributes are defined in the class body parts usually at the top, for legibility. Suppose we have the following code snippet : Python3 # Python code to demonstrate # the working of the sharing # of data variables # Creating a class class Geek_Class: geek = [] x = Geek_Class() y = Geek_Class() # Appending the values x.geek.append(1) y.geek.append(2) x.geek.append(3) y.geek.append(4) # Printing the values for x and y print(x.geek) print(y.geek) Output: [1, 2, 3, 4] [1, 2, 3, 4] It prints [1, 2, 3, 4] for x and [1, 2, 3, 4] for y. Suppose the output we want is [1, 3] for x and [2, 4] for y. We can get the desired output by the following ways: Method #1: By declaring them inside the __init__Declaring the variables inside the class declaration makes them class members and not instance members. Declaring them inside the __init__ method ensures that a new instance of the members is created alongside every new instance of the object, which is what we need. Python3 # Python code to demonstrate # the working of the sharing # of data variables # Creating a class inside __init__ class Geek_Class: def __init__(self): self.geek = [] x = Geek_Class() y = Geek_Class() # Appending the values x.geek.append(1) y.geek.append(2) x.geek.append(3) y.geek.append(4) # Printing the values for x and y print(x.geek) print(y.geek) Output: [1, 3] [2, 4] In the original code no value is assigned to list attribute after instantiation; so it remains a class attribute. Defining list inside __init__ works because __init__ is called after instantiation. Method #2: By creating the new list and storing the values in that. Python3 # Python code to demonstrate # the working of the sharing # of data variables # Creating a class class Geek_Class: geek =[] x = Geek_Class() y = Geek_Class() # Creating the new lists x.geek = [] y.geek = [] # Appending the values x.geek.append(1) y.geek.append(2) x.geek.append(3) y.geek.append(4) # Printing the values for x and y print(x.geek) print(y.geek) Output: [1, 3] [2, 4] Comment More infoAdvertise with us Next Article Python Attributes: Class Vs. Instance Explained S Shambhavi Singh 1 Follow Improve Article Tags : Python python-oop-concepts Python-OOP Practice Tags : python Similar Reads Data Classes in Python | Set 4 (Inheritance) Prerequisites: Inheritance In Python, Data Classes in Python | Set 3 In this post, we will discuss how DataClasses behave when inherited. Though they make their own constructors, DataClasses behave pretty much the same way as normal classes do when inherited. Python3 1== from dataclasses import data 2 min read Data Classes in Python | Set 4 (Inheritance) Prerequisites: Inheritance In Python, Data Classes in Python | Set 3 In this post, we will discuss how DataClasses behave when inherited. Though they make their own constructors, DataClasses behave pretty much the same way as normal classes do when inherited. Python3 1== from dataclasses import data 2 min read Pass Arguments to the Metaclass from the Class in Python Metaclasses in Python provide a powerful way to control the creation and behavior of classes. They act as the "class of a class" and allow you to customize class creation and behavior at a higher level. One interesting aspect of metaclasses is the ability to pass arguments from a class to its metacl 3 min read Python Attributes: Class Vs. Instance Explained In Python, attributes are properties associated with objects. They can be variables or methods that are defined within a class or an instance of a class. Understanding the difference between class and instance attributes is fundamental in object-oriented programming. Here's an explanation: Python At 4 min read Inheritance in Python | Set 2 Prerequisite : basics of inheritance in Python, Inheritance, examples of object, issubclass and super There are 2 built-in functions in Python that are related to inheritance. They are: 1. isinstance(): It checks the type of an object. Its syntax is: isinstance(object_name, class_name) It would retu 4 min read Inheritance in Python | Set 2 Prerequisite : basics of inheritance in Python, Inheritance, examples of object, issubclass and super There are 2 built-in functions in Python that are related to inheritance. They are: 1. isinstance(): It checks the type of an object. Its syntax is: isinstance(object_name, class_name) It would retu 4 min read Like