为什么 Python 生成器在 exec'd 脚本中将其范围与全局混淆?



好的,所以我在一个工具的配置脚本是exec 'd python脚本的环境中工作。exec调用是这样的:

outer.py:

exec(open("inner.py").read(), globals(), {})

现在,我想在exec 'd脚本中做一些相对基本的迭代。在这种情况下,当某些值不在白名单中时执行工作:

inner.py:

items = (
  'foo/bar',
  'foo/baz',
  'foof',
  'barf/fizz',
)
whitelist = (
  'foo/',
)
for key in items:
  try:
    # Not terribly efficient, but who cares; computers are fast.
    next(True for prefix in whitelist if key.startswith(prefix))
  # Do some work here when the item doesn't match the whitelist.
  except StopIteration:
    print("%10s isn't in the whitelist!" % key)

运行python inner.py得到预期结果:

      foof isn't in the whitelist!
 barf/fizz isn't in the whitelist!

这是奇怪的部分:运行python outer.py似乎表明解释器对生成器的作用域感到困惑:

Traceback (most recent call last):
  File "outer.py", line 1, in <module>
    exec(open("inner.py").read(), globals(), {})
  File "<string>", line 15, in <module>
  File "<string>", line 15, in <genexpr>
NameError: global name 'key' is not defined

其他注释:

  • 你可以在for循环中(在生成器运行之前)print(key)

  • outer.pyexec行中的locals()替换空字典解决了这个问题,但该代码超出了我的控制范围。

  • 我正在运行OS X构建的Python 2.7.2 (Mountain Lion).

确实很狡猾。

From the doc:

如果两个单独的对象分别作为全局对象和局部对象给出,代码将会像嵌入在类定义中一样执行。

(请注意,当您在模块级别运行exec(..., globals(), locals())时,这并不适用,因为globals() is locals(),即不是两个单独的对象)。

这表明您可以通过运行以下脚本简单地重现问题:

class A(object):
  items = (
    'foo/bar',
    'foo/baz',
    'foof',
    'barf/fizz',
  )
  whitelist = (
    'foo/',
  )
  for key in items:
    try:
      # Not terribly efficient, but who cares; computers are fast.
      next(True for prefix in whitelist if key.startswith(prefix))
      # Found!
      print(key)
    except StopIteration:
      pass

"好的,但是为什么我在这里得到错误?"

很高兴你问。

为什么不直接在里面添加一个for循环呢:

for key in items:
    for prefix in whitelist:
        if key.startswith(prefix):
            print(key)

我很确定你不会得到这个错误,它更容易阅读。

最新更新