redundant imports

max(01)* max2 at
Thu Mar 31 17:55:58 CEST 2005

Peter Hansen wrote:
> max(01)* wrote:
>> hi everybody.
>> suppose that imports and (because it 
>> uses names from both), and that imports
>> 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?
> You're mixed up about this whole idea.

that's why i am asking for advice here.

> You say first that "[code-1] uses names from both",
> which by definition means that it needs to import
> both.  (I say by definition because import is *how*
> a module gets names from another module.)
> Then you say that code-1 can choose not to include
> code-3 because some other module is including it...
> but so what?  That doesn't change the fact that
> code-1 needs names from code-3, and just because
> code-3 is imported elsewhere is no reason to think
> that code-1 magically gets its names too.  Should
> all modules magically see all names in all modules
> which are imported, even by other modules?

that's how inclusion works in c, which is why i asked for clarification 
about the differences between c-style includes and python-style imports.

now i can see that python-style import is more like the inclusion of 
declarations in c (the "names"), usually shipped as *.h header files.

the real *definition* of names is python (contents of variables, 
definition of functions, and so on) i guess is inserted later in the 
process of execution.

>  That
> would pretty much defeat most of the value of
> namespaces.
> Anyway, why this concern over so-called redundant
> imports?  The source code itself is not parsed
> and compiled all over again, and even the .pyc file
> is not re-read... once any module has imported a
> module any other import just retrieves a reference
> to that module from the sys.modules dictionary,
> which is practically a free operation.
> -Peter

my concern was motivated by a (clumsy) attempt to understand the 
difference of mechanism between the approach to modular programming in a 
more "traditional" language (c) and python.

thanks again for your couseling.



More information about the Python-list mailing list