I have this code which works as expected:
foo = 123
def fooBar():
    print(foo)   ### cannot find foo from local scope, checks prev and prints 123
    #foo = 987
    #print(foo)
fooBar() 
But when I change the code to the following,
foo = 123
def fooBar():
    print(foo)   ### blows up here... 
    foo = 987
    print(foo)
    
fooBar() 
it blows up on the print statement:
Traceback (most recent call last):
  File "python", line 9, in <module>
  File "python", line 4, in fooBar
UnboundLocalError: local variable 'foo' referenced before assignment
I know I can get around the problem using global foo inside my function, but the error I was expected was not on line number I was expecting. I was assuming it would still print 123 and then blow up when I try to assign another value to foo.
It seems as if Python parses fooBar() and knows I do an assignment of foo later in local function and then blows up on the print, instead of grabbing the global value? Is that right?
 
    