I am using python 2.7, but trying to make a code that checks if an object is a subclass of basestring compatible with python 3+ as well. I tried to follow the approach suggested here and found in the process a behavior that I do not understand
If I do:
def foo():
    try: basestring
    except NameError:
        print "a"
foo()
nothing happens.
If I slightly modify that code just inside the except:
def foo():
    try: basestring
    except NameError:
        print "a"
        basestring=str
foo()
Then "a" is printed.
I do not understand how adding something to the except block, can affect the triggering of the exception.
I checked the same code outside a function:
try:
    basestring
except NameError:
    print("a")
    basestring=str
but nothing gets printed in that case.
 
     
     
    