Simplifying imports?

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Tue Sep 13 09:31:21 CEST 2005


Terry Hancock wrote:
> On Monday 12 September 2005 10:09 pm, chapolim-colorado at bol.com.br wrote:
>> I like to keep my classes each in a separate file with the same name of
>> the class. The problem with that is that I end up with multiple imports
>> in the beginning of each file, like this:
>> 
>> from foo.Bar import Bar
>> from foo.Blah import Blah
>> from foo.Zzz import Zzz
>> 
>> What I'd like to do would be to replace it all by a single line:
>> 
>> from foo.* import *
>> 
>> Of course, that doesn't work, but is there a way to do something like
>> that?
> 
> Apparently "foo" is already a package defined using __init__.py,
> so you know about that part already.
> 
> Just change its contents to read:
> 
> from Bar import Bar
> from Blah import Blah
> from Zzz import Zzz
> 
> Then whenever you need to use these classes, you only need:
> 
> from foo import Bar, Blah, Zzz
> 
> or
> 
> from foo import *

Or, if the Bar, Blah and Zzz classes are small enough, there's no need to
put each of them in separate modules. Just put them in foo.py.

Concise structuring of classes and modules is not easy. Structure too much,
and you will not see the wood for the trees. Structure too little, and you
won't find things anymore. IMO, the Twisted project is an example of
excellent Python structuring.

Reinhold



More information about the Python-list mailing list