Considering the following code: I'm using a submodule allover my code and a special submodule (might be uber heavy to load) in only one function so I'm lazy importing:
import xml.etree
def y():
print(xml.etree.__name__)
def x():
print(xml.etree.__name__)
import xml.dom
print(xml.dom.__name__)
if __name__ == "__main__":
y()
x()
This will result in xml being unbound:
UnboundLocalError: local variable 'xml' referenced before assignment
(yes to fix it I can move the import up within the function OR do from xml import dom)
I'd love to know what happens here.
Apparently the imports are evaluated just before Python is entering the function.
What's the reasoning behind this?