[Tutor] Style question with classes and modules

Kent Johnson kent37 at tds.net
Thu Jul 19 21:27:49 CEST 2007

Tino Dai wrote:
> Hi there Everybody,
>        I have style question with importing of modules and classes. 
> Presently, I have several files importing several modules.
> #apacheModule
> import dbBase
> import dbCommon
> import miscBase
> My question is there any advantage to me wrapping them in a single file 
> (wrapper), and the importing a single file
> into that file (barring namespace collisons). Example shown below:
> In aggreateBase:
> import dbBase
> import dbCommon
> import miscBase
> In apacheModule:
> import aggreateBase
> # instead of
> #import dbBase
> #import dbCommon
> #import miscBase

Then you will have to refer to aggreateBase.dbBase.somethingInDbBase 
which is a bit awkward. BTW I think you mean aggregateBase?

> or perhaps even
> In aggreateBase:
> from dbBase import dbBase
> from dbCommon import dbCommon
> from miscBase import miscBase

This is pretty commonly done in a package __init__.py to promote names 
to package scope. So you might have
     class foo...
     class bar...

If __init__.py is empty then in a client you have to write e.g.
   from mypackage.stuff import foo

If __init__.py contains
   from stuff import *
   from nonsense import *

then a client can say
   from mypackage import foo

If your modules are related and belong together in a package then this 
can be handy. I wouldn't use it to group unrelated modules though.

Using from xx import * does obscure the true location of an object.

> The two advantages that I can see are, I don't need to type as much, and 
> there would be a speed up in the execution of code.

Why do you expect a speedup?

> Is there a reason why I shouldn't?

If they belong together, put them in a package and use __init__.py. if 
they don't belong together you are just obscuring the design for very 
little savings.


More information about the Tutor mailing list