importing down in code rather than at top of file.

alister alister.ware at ntlworld.com
Mon Aug 29 14:18:30 EDT 2016


On Mon, 29 Aug 2016 10:57:22 -0700, Tobiah wrote:

> Is it  worth while to defer the import of a large module that seldom
> gets used in the script?
> 
> 
> 	import sys import os
> 
> 	if hardly_ever_happens():
> 		
> 		import large_module large_module.do_task()
> 
> 
> 
> I imagine it takes a certain amount of processing power and memory to
> import a module, so it seems like I'd save those resources with the
> above pattern.
> 
> The down side would be that it's nice to see all of the imports at the
> top which would follow convention.  Should I care?
> 
> 
> Tobiah

That depends
does it actually make a significant delay to the loading of your 
application (have you timed it yet)

if the module does take a noticeable time to load do you want that 
additional delay in your rarely access conditions?

IMO start-up times for an application have to be significantly long 
before they become an issue unless they are a shout quick utility

example Libra office an take a few seconds to start but then will be 
running for a long time - the start-up time is not really significant.
a utility to perform a bulk rename you would not want to take 30 seconds 
to start if it would then complete in less than 1

remember the golden riules for optimisation

1) don't
2) (for advanced programmers only) Don't yet.




-- 
In a world without fences who needs Gates?



More information about the Python-list mailing list