[Tutor] general import VS importing inside a function

Fidel Sanchez-Bueno fidellira.6 at gmail.com
Sat Aug 29 04:34:03 CEST 2009


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??

here's a example:
case 1:

    # importing in the global scope
    import foo

    variable = "somethin here"

    def bar(value1, value2):
        return value1, value2 = foo.foo


Case 2:

    # importing inside a function

    variable = "somethin here"

    def bar(value1, value2):
        import foo
        return value1, value2 = foo.foo

   
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)

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" but i 
would actually like to read this from someone that knows about 
programming and not just me making asumptions...


salu2!
   


More information about the Tutor mailing list