Why is my class member shared among objects?

Viewed 23
class CoolList:

    def __init__(self, list=[]):
        self._list = list

    def append(self, item:str):
        self._list.append(item)


if __name__ == "__main__":
    a = CoolList()
    b = CoolList()
    a.append("test")
    print(a._list)
    print(b._list)
    # output: 
    # ['test']
    # ['test']

I have two objects of CoolList, a and b.

_list is not a static class member.

But when I append an item into a,

b also gets that item.

Why is that happening?

0 Answers
Related