How to avoid having class data shared among instances?

You want this:

class a:
    def __init__(self):
        self.list = []

Declaring the variables inside the class declaration makes them “class” members and not instance members. Declaring them inside the __init__ method makes sure that a new instance of the members is created alongside every new instance of the object, which is the behavior you’re looking for.

Leave a Comment