gpt4 book ai didi

python - 我收到错误 'redefined-outer-name'

转载 作者:太空狗 更新时间:2023-10-29 16:53:19 25 4
gpt4 key购买 nike

运行 lint 时,出现以下错误:

Redefining name 'tmp_file' from outer scope (line 38) (redefined-outer-name)

这是我在该行中的代码片段:

tmp_file = open('../build/' + me_filename + '.js','w')

最佳答案

发生这种情况是因为您的本地名称与全局名称相同。当然,局部名称优先,但它隐藏了全局名称,使其不可访问,并给读者造成混淆。

解决方案

更改本地名称。或者可能是全局名称,无论什么更有意义。但请注意,全局名称可能是公共(public)模块接口(interface)的一部分。本地名称应该是本地名称,因此可以安全更改。

除非...您的意图是让这些名称相同。然后您需要在本地范围内将名称声明为 global:

tmp_file = None

def do_something():
global tmp_file # <---- here!
tmp_file = open(...)

如果没有 global 声明,本地 tmp_file 将与全局文件无关。因此警告。

关于python - 我收到错误 'redefined-outer-name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24999937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com