foo = 'asdf'
bar = 'asdf'
foobar = 'asdf'
def test():
def get_foo():
print 'foo = ' + foo
def get_bar():
try:
print 'bar = ' + bar
except Exception as e:
print 'nobody set bar :('
def get_foobar():
try:
foobar
except Exception as e:
print e
foobar = "nobody set foobar :("
print foobar
print 'trying to get foo'
get_foo()
print '\ntrying to get bar'
get_bar()
print '\ntrying to get foobar'
get_foobar()
test()
When I run this, I get this:
trying to get foo
foo = asdf
trying to get bar
bar = asdf
trying to get foobar
local variable 'foobar' referenced before assignment
nobody set foobar :(
I set up three variables: foo, bar, foobar. Then, I print them out using three nested functions: get_foo, get_bar, and get_foobar.
get_foo simply prints foo get_bar prints bar and checks for exceptions get_foobar uses a try-except to ensure that foobar is set before printing it.
Somehow, get_foobar fails?!