I have two files, blah.py containing
def blah():
print("External blah")
return "6"
and test.py containing
def blah():
print("Internal blah")
return "5"
if __name__ == '__main__':
try:
from blah import blah
except:
pass
num = blah()
print(num)
When I run this, I get
External blah
6
When I make blah.py inaccessible by renaming it to blaat.py, I get
Internal blah
5
Now, if I change test.py to
def blah():
print("Internal blah")
return "5"
def blater():
try:
from blah import blah
except:
pass
num = blah()
print(num)
if __name__ == '__main__':
try:
from blater import blater
except:
pass
blater()
it works when blah.py is accessible, but I get an error message when blah.py is not accessible:
Traceback (most recent call last):
File "./test.py", line 27, in <module>
blater()
File "./test.py", line 16, in blater
num = blah()
UnboundLocalError: local variable 'blah' referenced before assignment
Why is that? What subtle difference in calling a function am I missing?