0

I am following 6.00.1x from MITx and the slide (as attached below) confuses me. Specifically, why does h(y) comes back as unbound local error.

The following is a further explanation posted:

n += y, which is the same as n = n + y, will fail if n hasn't been defined locally or already declared nonlocal or global because

  • the interpreter first evaluates the right side of the = so finds n in the global scope (& thus readable) and adds y to that, then
  • the interpreter tries to bind the result of global n + y to n on the left of the =. But because n isn't local & hasn't been declared global this attempted binding creates the error.

The first bullet point is straightforward. In the second one, I understand that the interpreter is trying to bind the result of n + y to n. n is not local and that's fine; but it has been declared in the global scope hasn't it? Specifically, x=5 declared the variable.

The issue here seems to be that the function is trying to bind n (or in this case x) to the global scope, but that is not allowed - I understand that binding results from local scope to global is not allowed, but doesn't Python instantiate n/x in the local scope automatically and bind n+y to that instead?

enter image description here

  • 1
    Within a function, a variable is either local or global (or in Python 3 it can also be nonlocal, but forget that for now). If the function never assigns to a variable, but only reads it, then it's global. If the function assigns to the variable, anywhere in the function, then the variable is local, everywhere in the function. To force it to be global, use a `global` declaration inside the function. – Tom Karzes Jun 23 '19 at 19:40
  • 1
    The text you're quoting from is wrong. Your understanding is right: `x` is instantiated in the local scope and set to `x + 1`. The real problem is that evaluating `x + 1` causes an error, precisely because there is now a local variable `x` but it has no value yet. – melpomene Jun 23 '19 at 19:44
  • @melpomene So this is like a circular definition? `x` has no value in the local scope, but doesn't Python then read it off the global scope (i.e. `x=5`)? – Constantly confused Jun 23 '19 at 19:53
  • 1
    @DanielMak Read my comment. I explained this. It doesn't change scope in the middle of a function. If it's local, then it's local *everywhere* in the function, from start to finish. And if you try to read a local variable before assigning to it, it's an error. It's very simple. – Tom Karzes Jun 23 '19 at 19:54
  • 1
    If the function assigns to `x` anywhere, it is created in the local scope and all occurrences of `x` refer to that local variable. `x = x + 1` is effectively circular: To find the initial value of `x`, we first must have `x`. Only if the function doesn't assign to `x` can it "see" the global `x`. – melpomene Jun 23 '19 at 19:55
  • Sorry @TomKarzes I didn't understand your comment at first but now in combination of comment from melpomene the whole thing makes sense now; thank you both – Constantly confused Jun 23 '19 at 20:25

0 Answers0