测试功能:
def test_dicts(first_dict, second_dict):
print('\nbefore first_dict_:', first_dict)
print('before second_dict:', second_dict)
del second_dict[2]
print('\nafter first_dict_:', first_dict)
print('after second_dict:', second_dict)
测试数据:
dicts = {1: {11:11}, 2: {22:22}}
让我们测试一下:
>>> tmp.test_dicts(dicts, dicts)
before first_dict_: {1: {11: 11}, 2: {22: 22}}
before second_dict: {1: {11: 11}, 2: {22: 22}}
after first_dict_: {1: {11: 11}}
after second_dict: {1: {11: 11}}
del second_dict[2]
明确表示要从second_dict
删除密钥,但由于某种原因,Python也会从first_dict
删除此密钥。为什么会这样?
答案 0 :(得分:1)
让我们尝试回答。
>>> a = "hello"
>>> b = a
>>> bool(id(a) == id(b))
True
因此,即使它们具有不同的变量名,它们仍然是同一个对象。