Python:NameError:在封闭范围内赋值之前引用了自由变量“re”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16545262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:57:37  来源:igfitidea点击:

Python: NameError: free variable 're' referenced before assignment in enclosing scope

python

提问by robert

I have a strange NameError in Python 3.3.1 (win7).

我在 Python 3.3.1 (win7) 中有一个奇怪的 NameError。

The code:

编码:

import re

# ...

# Parse exclude patterns.
excluded_regexps = set(re.compile(regexp) for regexp in options.exclude_pattern)

# This is line 561:
excluded_regexps |= set(re.compile(regexp, re.I) for regexp in options.exclude_pattern_ci)

The error:

错误:

Traceback (most recent call last):
  File "py3createtorrent.py", line 794, in <module>
    sys.exit(main(sys.argv))
  File "py3createtorrent.py", line 561, in main
    excluded_regexps |= set(re.compile(regexp, re.I) for regexp in options.exclude_pattern_ci)
  File "py3createtorrent.py", line 561, in <genexpr>
    excluded_regexps |= set(re.compile(regexp, re.I) for regexp in options.exclude_pattern_ci)
NameError: free variable 're' referenced before assignment in enclosing scope

Note that line 561, where the error occurs, is the secondline in the code above. In other words: reis nota free variable. It is simply the regular expression module and it can be referenced perfectly fine in the firstline.

请注意,发生错误的第 561行是上面代码中的第二行。换句话说:re不是自由变量。它只是正则表达式模块,可以在第一行完美引用。

It seems to me that the reference to re.Iis causing the problem, but I don't see how.

在我看来,引用re.I是导致问题的原因,但我不明白是怎么回事。

采纳答案by alexis

Most likely, you are assigning to re(presumably inadvertently) at some point belowline 561, but in the same function. This reproduces your error:

最有可能的是,您第 561 行下方的re某个点分配给(可能是无意中),但在相同的函数中。这会重现您的错误:

import re

def main():
    term = re.compile("foo")
    re = 0

main()

回答by SingleNegationElimination

"free variable" in the traceback suggests that this is a local variable in an enclosing scope. something like this:

回溯中的“自由变量”表明这是封闭范围内的局部变量。像这样:

 baz = 5

 def foo():
     def bar():
         return baz + 1

     if False:
          baz = 4

     return bar()

so that the bazis referring to a local variable (the one who's value is 4), not the (presumably also existing) global. To fix it, force bazto a global:

所以 指的baz是局部变量(值是 4 的那个),而不是(大概也存在的)全局变量。要修复它,请强制baz使用全局:

 def foo():
     def bar():
         global baz
         return baz + 1

so that it won't try to resolve the name to the nonlocal version of baz. Better yet, find where you're using rein a way that looks like a local variable (generator expressions/list comprehensions are a good place to check) and name it something else.

这样它就不会尝试将名称解析为 baz 的非本地版本。更好的是re,以一种看起来像局部变量的方式(生成器表达式/列表推导式是检查的好地方)找到您正在使用的位置,并将其命名为其他名称。

回答by Yasin Z?hringer

The other explanations are perfect, but let me add another variant which I just discovered. Due to Python 3 preferring iterators over 'physical' lists one has to be more careful:

其他解释是完美的,但让我添加另一个我刚刚发现的变体。由于 Python 3 更喜欢迭代器而不是“物理”列表,因此必须更加小心:

def foo():
    re = 3
    faulty = filter(lambda x: x%re, range(30))
    del re
    return faulty
list(foo())

The filter expression only gets evaluated after the return statement in the last line, in particular after del re. Hence the last line explodes with the error:

过滤器表达式仅在最后一行的 return 语句之后进行评估,特别是在del re. 因此,最后一行因错误而爆炸:

NameError: free variable 're' referenced before assignment in enclosing scope