1

Consider the following code:

foo=None
def bar():
    print(foo, flush=True) # UnboundLocalError
    print('MARK')
    del foo

print(foo)
bar()
#print(foo)

Basically I have a function that calls del in it. However, when I run the program, I get UnboundLocalError for that 'foo' referenced before assignment. If I comment out the del line, the exception is gone. I suppose the del should be called exactly on the line it is on, so that the variable is invalidated only after that. But it doesn't seem to be the case.

Is this expected behavior? (Specifically, I'm using Python 3.6.2)

YiFei
  • 1,752
  • 1
  • 18
  • 33

0 Answers0