Simplifying imports?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Sep 13 14:31:40 EDT 2005


On Mon, 12 Sep 2005 20:09:31 -0700, chapolim-colorado 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?

"Doctor, it hurts when I do this."

"Then don't do that."

If putting one class per file causes you pain, then don't put one class
per file. The idea of packages is to make things easier for you, not
harder -- if you are having to fight the import mechanism, then perhaps
you need to rethink your design.

eg if you have a good reason for requiring one class per file, then one
possible work around would be to define a single "header" module
containing all those "from foo.Bar import Bar" statements, and then in
your actual module(s) call "from header import *". Watch out for circular
imports though.


-- 
Steven.




More information about the Python-list mailing list