Search code examples
pythonpython-closures

Why I encounter an UnboundLocalError when I wrote a python closure, but in another similar code snippet I didn't?


I have two code snippets here.

The first one, function create

def creat(pos):
  
  def move(direction, step):
    new_x = pos[0] + direction[0]*step
    new_y = pos[1] + direction[1]*step
    pos[0] = new_x
    pos[1] = new_y

    return pos
  
  return move


player = creat([0,0])
print('after move along x 5 step, the new position is:'+str(player([1,0], 5)))
print('after move along y 10 step, the new position is:'+str(player([0,1], 10)))

and the second, function wrapper

def wrapper(x):
  def func():
    temp = x+1
    x = temp
    return x
  return func

counter = wrapper(0)
for i in range(5):
  print(counter())

The first one works well, but the second raises an error about variable x: local variable x referenced before assignment. I can understand literally what this error means, but I think the variable x should be the same as variable pos in the former snippet. Why the pos is ok but the x not?


Solution

  • The assignment x = temp creates a new local variable that shadows the non-local x defined by wrapper. Even though temp = x + 1 precedes it at run time, it still refers to the local variable x that isn't yet initialized.