where to import

andrea crotti andrea.crotti.0 at gmail.com
Thu Feb 17 12:27:17 EST 2011


2011/2/17 Chris Rebert <clp2 at rebertia.com>

> Yes, of course. Importing a module multiple times (as would happen if
> fun() is called multiple times) is obviously slower than just
> importing it once, although the 2nd and subsequent imports will be
> faster as Python will just return another reference to the previously
> imported instance of the module rather than importing it again from
> scratch. There are also local variable vs. module-global variable
> speed differences involved.
>
> However, "Premature optimization is the root of all evil." Have you
> checked whether the imports are taking a significant amount of
> execution time? Odds are that they aren't. When optimizing, do so on
> the basis of empirical data; run your program under a profiler, and
> let the results guide your optimization process.
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com


Well no I wasn't really worried about performances.
I just thought that if an external module is really almost never used, it
might make sense to import it only when it's really needed.

Anyway if it's against the holy pep8 then it's fine :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110217/0cf22353/attachment-0001.html>


More information about the Python-list mailing list