Where to import modules?

Andreas Neudecker a.neudecker at uni-bonn.de
Fri Sep 26 13:33:53 EDT 2003


Hi.

Thank you for your opinions!

Good to look at it from different sides.
So I think one could sum it all up as:

- Best place for importing modules is at the top of the file.
- if optimisation requires or programmer desires to import specific 
modules somewhere else, it is a good idea to add a comment at the top, 
where all the other modules are imported.
- for optimasation importing inside modules/functions will only make 
sense if loading the module takes time (try rpy, the wrapper for R!) and 
is not used every time the program runs.

I liked Peter Hansen's remark on optimasation:
"Basically doing this is an optimization, and you shouldn't be 
optimizing before the program is working, and you shouldn't be 
optimizing without a real need for it.  (That is, just because it's a 
little faster doesn't mean it's worth doing.)"

But also the contrary remark by "JCM":
"Aside from being an optimization, it's also good for documentation.
If you see an import at the top of a file it might not be immediately
clear how or why the module is being used.  It's similar reasoning to
why you might define variables in the narrowest scope posible."


Thanks everybody.

Regards


Andreas






More information about the Python-list mailing list