【发布时间】:2021-12-29 04:16:58
【问题描述】:
我编写了以下程序,其中我试图修改local symbol table 中的dictionary values:
def modify_locals():
str = True
print(locals())
locals()['str'] = False
print(locals())
modify_locals()
输出:
{'str': True}
{'str': True}
请告诉我为什么locals()['str'] = False 不更新local symbol table?
【问题讨论】:
-
docs.python.org/3/library/functions.html#locals
The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter. -
首先不要在类名上命名变量
-
简单地说,“这里有龙”。不要这样做。真的。
标签: python python-3.x function scope local-variables