辞書型インスタンス変数の更新時に、特定のインスタンスの変数のみを更新したい

python

1class Example:2 def __init__(self, **dic):3 self.dic = dic 4 5 def update_dic(self):6 self.dic['A']['d'] = self.dic['A'].pop('a')7 8dic = {9 'A': {'a': 1, 'b':2, 'c': 3},10 'B': {'a': 1, 'b':2, 'c': 3}11}12 13lis = [14 Example(**dic),15 Example(**dic)16]17 18lis[0].update_dic()19 20 21print(lis[0].dic)22print(lis[1].dic)23print(dic)24 25### 出力結果26{'A': {'b': 2, 'c': 3, 'd': 1}, 'B': {'a': 1, 'b': 2, 'c': 3}}27{'A': {'b': 2, 'c': 3, 'd': 1}, 'B': {'a': 1, 'b': 2, 'c': 3}}28{'A': {'b': 2, 'c': 3, 'd': 1}, 'B': {'a': 1, 'b': 2, 'c': 3}}29 30### 期待される出力31{'A': {'b': 2, 'c': 3, 'd': 1}, 'B': {'a': 1, 'b': 2, 'c': 3}}32{'A': {'a': 1, 'b':2, 'c': 3}, 'B': {'a': 1, 'b': 2, 'c': 3}}33{'A': {'a': 1, 'b':2, 'c': 3}, 'B': {'a': 1, 'b': 2, 'c': 3}}34

コメントを投稿

0 コメント