如何从一张单独的纸条中永久地改变模块中的值

2024-04-18 04:02:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个模块english.py,它包含一个字典monograms。与每个键关联的值的类型是str,我想从一个单独的脚本中将与每个键关联的值的类型永久更改为int。 我尝试用以下方法模拟对this question的响应:

import english
english.monograms['the'] = int(english.monograms['the'])
print(type(english.monograms['the']))

产生预期的:<class 'int'>

但是,当我用注释掉的第2行重新运行代码时:

import english
#english.monograms['the'] = int(english.monograms['the'])
print(type(english.monograms['the']))

我收到<class 'str'>

如果没有一个复杂的函数来读取、解析和重写模块,我如何使这个更改永久存在?你知道吗


Tags: 模块thepyimport脚本类型字典english