0
class class1():
    def __init__(self,dict1={1:2,3:4}):
        self.dict1=dict1
        return

list1=[]
list1.append(class1())
list1.append(class1())
list1.append(class1())
list1[0].dict1[1]="test"
print(list1[0].dict1,list1[1].dict1,list1[2].dict1)

Output: {1: 'test', 3: 4} {1: 'test', 3: 4} {1: 'test', 3: 4}

Whenever I change an element of the first term of "list1", say the value of key "1" from "2" to "test", every element adopts that same change.

How do I make it so that only the first element gets altered?

Thanks in advance.

1 Answers1

0

Try this:

class class1():
    def __init__(self,dict1={1:2,3:4}):
        self.dict1=dict1.copy()
        return

Reza
  • 1,945
  • 1
  • 9
  • 17