I am maintaining a few Python packages, and on systems where dependencies are not fully satisfied, functions which may otherwise work, fail due to global scope imports:
import numpy as np
def lala(in):
out = max(in)
return out
def fufu(in):
out = np.mean(in)
return out
So for instance here I cannot use lala()
if I do not have numpy, ven if lala does not use numpy.
Of course, ideally dependencies would be managed correctly, still, it would make for a more robust package if functions failed only when they had to fail.
Is there any reason why imports are almost never done in function scope? Is this reason just reducing the number of lines?