Where to place imports
Diez B. Roggisch
deets at nospam.web.de
Fri Jan 23 19:04:42 CET 2009
Steve Holden schrieb:
> Diez B. Roggisch wrote:
>> The only valid reason for doing imports inside functions is if you
>> otherwise get into circular dependency hell, or have modules that need
>> some manipulation of the sys.path before they actually can be imported.
>> This is never true for system modules, and certainly to avoid if possible.
> Well there is also the somewhat obscure point that an import of specific
> names inside a function makes them local, which *might* make a
> performance difference with tight loop nesting inside - though obviously
> the need for such will best be determined by benchmarking:
But that could be mitigated using aliasing, which is a more general
optimization technique, as it works not only for imported names but also
for e.g. bound methods, or any other chained lookups.
More information about the Python-list