How can “NameError: free variable 'var' referenced before assignment in enclosing scope” occur in real code?

大城市里の小女人 提交于 2019-12-23 09:06:07

问题


While I was hanging out in the Python chatroom, someone dropped in and reported the following exception:

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

I'd never seen that error message before, and the user provided only a small code fragment that couldn't have caused the error by itself, so off I went googling for information, and ... there doesn't seem to be much. While I was searching, the user reported their problem solved as a "whitespace issue", and then left the room.

After playing around a bit, I've only been able to reproduce the exception with toy code like this:

def multiplier(n):
    def multiply(x):
        return x * n
    del n
    return multiply

Which gives me:

>>> triple = multiplier(3)
>>> triple(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in multiply
NameError: free variable 'n' referenced before assignment in enclosing scope

All well and good, but I'm having a hard time working out how this exception could occur in the wild, given that my example above is

  1. Pretty stupid
  2. Unlikely to happen by accident

... but obviously it does, given the report I mentioned at the start of this question.

So - how can this specific exception occur in real code?


回答1:


Think of a more complex function where n is bound depending on some condition, or not. You don't have to del the name in question, it also happens if the compiler sees an assignment, so the name is local, but the code path is not taken and the name gets never assigned anything. Another stupid example:

def f():
    def g(x):
        return x * n
    if False:
        n = 10
    return g


来源:https://stackoverflow.com/questions/24707379/how-can-nameerror-free-variable-var-referenced-before-assignment-in-enclosin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!