Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 705
Zero Piraeus Avatar asked Jul 11 '14 22:07

Zero Piraeus


1 Answers

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
like image 96
BlackJack Avatar answered Sep 30 '22 04:09

BlackJack