I have a very small sample of code that uses a try:except block and I'm a bit confused as to why it returns hi as not being defined:
def myFunction():
try:
pass
except:
hi = "bunk"
print(hi)
hi = "blah"
myFunction()
I would have expected it'll remain as "blah" unless it fell in the exception, in which case it'll turn into "bunk". When debugging, the IDE returns "blah" as the value of hi, but throws an exception when it tries to print it.
It appears as though hi becomes a local variable within the function, so once it gets to the print statement, it fails with an error that it's not defined as the try statement didn't define it. Is that what is happening?
I realize that if it's not defined, it'll throw the UnboundLocalError, but the following works fine:
def myFunction():
## try:
## pass
## except:
## hi = "bunk"
print(hi)
hi = "blah"
myFunction()