Ing*_*soc 2 python debugging function pycharm
我不完全确定这种行为是否符合预期,但这绝对是很奇怪的。当你有这样的代码时:
def a_function():
if a_list != some_other_list:
print(a_list)
Run Code Online (Sandbox Code Playgroud)
它工作得很好,我没有遇到任何问题。但是,如果将其更改为:
def a_function():
if a_list != some_other_list:
a_list = some_other_list
Run Code Online (Sandbox Code Playgroud)
突然,出现了一个问题,提示a_list第 2 行是未解析的引用。为什么if语句中的内容会影响能否a_list解决呢?这种事情正常吗?这可能是 Python 3.6.1 或 PyCharm(社区版 2017.1.5)中的错误吗?任何澄清这一点的帮助将不胜感激。
只要您只访问函数中非本地变量,只要在外部作用域中的某个位置找到它们,一切都可以正常工作。但是一旦你分配给一个变量,它就成为你的函数的局部变量。Python 将不再在外部作用域中查找变量名称。所以当你包括:
a_list = some_other_list
Run Code Online (Sandbox Code Playgroud)
该行:
a_list != some_other_list
Run Code Online (Sandbox Code Playgroud)
将会失败,因为它不再在你的函数之外寻找a_list,如果你稍后在函数中定义它,它是一个UnboundLocalError或类似的。
如果您感兴趣的话,我确实在另一个答案中详细介绍了。
如果您只想比较相等性更改=(分配)到==(相等性检查)。
| 归档时间: |
|
| 查看次数: |
1986 次 |
| 最近记录: |