我有一个全局dictionary变量,我试图调用一个递归函数,该函数根据它要查找的键向下移动几个级别,然后更改该值。在
我想知道为什么当我在函数中改变全局变量的一个分支时它没有改变它。在
所以我的递归函数如下所示:
def update_dict_with_edits(base_system_dict, changed_json_dict):
name = changed_json_dict["base_system"]["name"]
if "base_system" not in base_system_dict:
return
sub_dict = base_system_dict["base_system"]
if name == sub_dict["name"]:
print(name)
print("found it and updating")
sub_dict = changed_json_dict
# even if I print out here the_global_dict_object it is unaltered
# print(the_global_dict_object)
return
if "sub_systems" not in sub_dict:
return
for d in sub_dict["sub_systems"]:
update_dict_with_edits(d, changed_json_dict)
我称之为:
^{pr2}$我用的是烧瓶,但我觉得没那么重要。在
您是changing a name, not mutating a reference:
相反,请更新
^{pr2}$base_system_dict
中的引用:相关问题 更多 >
编程相关推荐