What's wrong with this code? (UnboundLocalError: local variable referenced before assignment)

Joshua Landau joshua.landau.ws at gmail.com
Mon Jun 24 16:19:01 EDT 2013


Here's a little test to make sure you understand (this is one of the
most confusing parts of Python's closures in my opinion):

foo = "I'm foo!"

def working():
    print(foo)

def broken():
    print(foo)

    if False: # There's no way this could cause a problem!
        foo = "This will *never* happen"



More information about the Python-list mailing list