Categories
Mastering Development

add items to the list of an object

class bag:

    items = []

    def add_item(self,x):
        self.items.append(x)


if __name__ == "__main__":

    bag1 = bag()
    bag2 = bag()

    bag1.add_item("water")
    print(bag2.items)
    

I’m trying to add water to the bag1 but I have no idea why "bag1.add_item("water")" will affect bag2

Leave a Reply

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