I am often tempted to import modules in narrow contexts where they are needed. For example, in the body of a function that uses a module. In this case, the import statement can be executed many times.
Besides stylistic issues, what is the cost of doing this?
Take a look at the explanation on this site:
https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead
, Python , , . , , .
import, Python ( import), reload. ( ) import .
import
reload
; import . , , .
, :
, , , , 20 , , .. , .
, ipython python 2.7.3, , 450 nano :
%%timeit import numpy pass
100000000 , 3: 11,3
%%timeit import numpy import numpy pass
1000000 , 3: 465