redundant imports

Tim Jarman tmj at SPAMLESSjarmania.com
Wed Mar 30 08:22:42 EST 2005


max(01)* wrote:

> hi everybody.
> 
> suppose that code-1.py imports code-2.py and code-3.py (because it uses
> names from both), and that code-2.py imports code-3.py.
> 
> if python were c, code-1.c should only *include* code-2.c, because the
> latter in turns includes code-3.c.
> 
> inclusion of modules in c is a purely preprocessing textual matter
> (compilation is deferred to after the fact), i guess, so that such
> things are possible. import of modules in python is a different beast,
> so the "redundancy" is (i think) necessary.
> 
> any comment/suggestion/idea?
> 
> bye
> 
> macs

It's not as redundant as it looks. Once a module has been imported it goes
into sys.modules and any subsequent imports refer to that original import,
so the overhead of reading and parsing the file is only incurred once. As
you're probably aware, Python also caches compilation results in *.pyc
files; it will only compile the imported module if it changed since the
last compilation.

Check out the docs for the full skinny, in particular
http://www.python.org/doc/2.4/ref/import.html

HTH,
Tim J

-- 
Website: www DOT jarmania FULLSTOP com



More information about the Python-list mailing list