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

问题描述

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

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"}

解决方法

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

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

See here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...