In Python, what happens when you import inside of a function? -


this question has answer here:

what pros , cons of importing python module and/or function inside of function, respect efficiency of speed , of memory?

does re-import every time function run, or perhaps once @ beginning whether or not function run?

does re-import every time function run?

no; or rather, python modules cached every time imported, importing second (or third, or fourth...) time doesn't force them go through whole import process again.

does import once @ beginning whether or not function run?

no, imported if , when function executed.

as benefits: depends, guess. if may run function , don't need module imported anywhere else, may beneficial import in function. or if there name clash or other reason don't want module or symbols module available everywhere, may want import in specific function. (of course, there's from my_module import my_function f cases.)

in general practice, it's not beneficial. in fact, python style guides encourage programmers place imports @ beginning of module file.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -