"Fidel Sanchez-Bueno" <fidellir...@gmail.com> wrote
What is the best aproach when it comes to import??, is allways better to
make all the import calls in the global scope, making the overall runtime
of the program better because the program is not going to import
something everytime a function is called, or is better to make specific
import's inside a function making the function completely portable??
The general concensus is to keep all imports together at the top of the
module.
I do occasionally put an import inside a function but only if
a) its in an exceptioon block that I don;t expect to be used AND
b) the module takes significant time to load and I care about performance
Those two things don't happen very often so 99% of the time all imports
go at the top.
and another thing, making specific import, makes the program less, how
you said this "RAM eater" (sorry my native language is spanish and i dont
now how to translate "cosume menos recursos" google says "cosumes less
resources", but anyway i hope you get it)
Unless the module has huge data structures populated within it thats
unlikely to be a significant issue. Usually the memory consumption comes
from the main execution code where the data is defined not the module code.
On a huge project that might be an issue but not on the average Python
project.
I say this because, for example when i'm in the interactive shell and
import big things like Scipy, and NumPy, python takes 3 to 5 seconds to
respond, and if am only going to use like 4 or 5 function inside NumPy i
think is better to say just "from Numpy import foo, bar, qux etc"
Thats a different question. :-)
If you are only using a few names from the module the "from x imporrt y "
style is good.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor