[Tutor] Import multiple classes from one module

Michael P. Reilly arcege@speakeasy.net
Sat, 29 Sep 2001 07:31:33 -0400


On Sat, Sep 29, 2001 at 02:08:14AM -0500, runsun wrote:
> I have a module containing several classes, say,
> c1, c2, c3.
> 
> It seems that I have to import each class one by one like:
> 
> from mymodule import c1
> from mymodule import c2
> from mymodule import c3
> 
> What if there are 30 classes in that module that
> I wanna import ? Should I make 30 imports or 
> there's a better way to do it ?

If you have that many, then it might be better to import the module
itself (not the classes) and reference the classes through the module.

  import mymodule

  c1inst = mymodule.c1()
  class c2subclass(mymodule.c2):
    def __init__(self, arg):
      mymodule.c2.__init__(self)
      self.arg = arg

Also, if you have a "few" names to import, you can separate them with
commas.

  from mymodule import c1, c2, c3

Good Luck,
  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |
| Ailment info: http://www.speakeasy.org/~arcege/michaelwatch.html     |