When in doubt, I typically place my import statements at the top of the module. Often, this cuts down on repetition, which is nice. Is there a performance downside, though, in the case where only a single function (or class) requires the import?
does the following only import when the function is called?
def func():
from task import test
If so, I imagine that might be a slight efficiency. I also assume that you could get some added points for faster garbage collection and variable scoping, since the imported objects would not be added to the global dictionary. As another poster nicely put it:
This is mostly due to variable look-up. Looking up a variable in the global scope requires a dictionary look-up. In contrast, the compiler determines local names statically and references them by index, so no dictionary look up is required.
Are those fair assumptions are am I totally off base?
Thanks