Python 3.x Python变量引用不起作用

Python 3.x Python变量引用不起作用,python-3.x,Python 3.x,Python 3.4.4, Windows 8.1 我在python中处理变量时遇到了一些麻烦。我有一个存储参考词典的程序。这存储在static.py文件中。当我从另一个.py文件中提取引用字典时,它正在更改原始引用 static.py File dictionary_a = {'a': 'The sky is blue and {}.', 'b': 'Second sentence'} 我希望从static.py引用字典,但保留原始变量不变。因此,理想的输出将是 >> 'The

Python 3.4.4, Windows 8.1

我在python中处理变量时遇到了一些麻烦。我有一个存储参考词典的程序。这存储在static.py文件中。当我从另一个.py文件中提取引用字典时,它正在更改原始引用

static.py File

dictionary_a = {'a': 'The sky is blue and {}.', 'b': 'Second sentence'}
我希望从static.py引用字典,但保留原始变量不变。因此,理想的输出将是

>> 'The sky is blue and Yellow.'
>> 'The sky is blue and {}.'

我找到了答案。当您尝试创建字典的副本时,您需要在python中显式地创建它,因为使用dictionary_b=dictionary_a引用同一个字典

要创建副本,正确的方法是:

字典


dictionary_b=dictionary_a.copy

dictionary_a={'天是蓝的,{}.}不是一个dict,而是一个集合。这在这里可能无关紧要,但它的名称令人困惑。上面的代码无法工作,因为您正在对集合调用format,这会导致AttributeError;它肯定没有给出您显示的输出。这可能是真的,但您的原始代码没有按照您所说的那样执行,因此通常很难提供帮助,例如dictionary_b['a']。格式“Yellow”对字典没有副作用,所以打印出dictionary_a['a']仍然是“天空是蓝色的,{}”。
>> 'The sky is blue and Yellow.'
>> 'The sky is blue and {}.'