我在Python 3.3.1(win7)中有一個奇怪的NameError。Python:NameError:在封閉範圍內賦值之前引用的自由變量're'
的代碼:
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)
錯誤:
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
注意,行561,其中發生了錯誤,是在上面的代碼中的秒線。換句話說:re
是而不是一個自由變量。它只是正則表達式模塊,它可以在第一行行中被完美地引用。
在我看來,提到re.I
是造成這個問題,但我不知道如何。
如果您從行561中刪除're.I',錯誤消失嗎?我懷疑它 - 這個問題很可能是由於你在問題中遺漏了某些東西造成的。 – alexis 2013-05-14 14:06:56
嘗試提供一個http://sscce.org/。 – glglgl 2013-05-14 14:11:33
@alexis你是對的。如果我刪除're.I',錯誤不會消失。 – robert 2013-05-14 14:14:52