0

即使仅保存在不同的对象上,更改也会出现在主对象上?

A dict:
transportation = {"car":"ford", "coche":"fiat"}
The changes:
x = transportation.setdefault("carro", "BMW")
Output of the object where changes were stored(x):
BMW
Output of the original dict:
{"car":"ford", "coche":"fiat", "carro", "BMW"}
4

1 回答 1

1

字典是可变对象。这意味着您正在分配对同一字典的引用。而是尝试

x = transportation.copy().setdefault("carro", "BMW")

看这里

于 2021-02-23T21:53:24.580 回答