I have the following modules:
main.py
import my_import
my_import.a_func()
my_import.py
FOO = "foo"
BAR = []
def a_func():
BAR.append("bar") #ok
FOO = FOO + "foo" #UnboundLocalError:
#local variable 'FOO' referenced before assignment
This is probably due to the importing, but how?
[EDIT]
From the answers I get it is not the importing that is the crulpit, but the follwing is still weird:
FOO = "foo"
BAR = []
def a_func():
BAR.append("bar")
print(FOO)
a_func()
--> prints "foo"
FOO = "foo"
BAR = []
def a_func():
BAR.append("bar")
print(FOO)
FOO = FOO + "foo"
a_func()
--> fails with "UnboundLocalError: local variable 'FOO' referenced before assignment" AND DOES NOT PRINT "foo"
Looks like the interpreter is looking for assignments in the current scope before it actually runs the code.