[Tutor] import question

Glen Wheeler wheelege@tsn.cc
Wed, 4 Jul 2001 21:10:59 +1000


> On  0, Glen Wheeler <wheelege@tsn.cc> wrote:
> >   Hey Tutorians,
> >
> > <...snip!...>
> >
> > and when I try to run mainmodule pythonwin spits with an error in aux1 -
> > saying mainmodule has no attribute uniclass, which is just wrong.  If I
run
> > it again then it dies when I try to use a class from aux1 (or any of the
> > other modules), which makes sense if the original import died.
>
> Look closely what happens: in the main module, you do the import *before*
> your class definitions. So *at the time the aux1 module is run*, uniclass
> doesn't exist yet!
>

  *clonk!*  Silly me :)

> Usually, you can solve this though by doing the imports in the main module
> at the bottom of the file, when the classes have already been defined.
>

  Yes...but...urgh so not-nice...hmmm I'm sure you know what I'm trying to
express here :).

> Also, in many cases not using "from...import" works, because the class is
> only used inside of functions, so it's only looked up when the function is
> run. Lots of problems can be avoided by never using "from...import" :-)
>

  Indeed, yet more reasons to not use it :)

> Generally "circular imports" are a bit dodgy and sometimes a sign of bad
> design, at least, I often have the feeling that there's a cleaner design
> lurking just outside my range of thought somewhere.
>

  Bad design eh?  Hmmm sure sounds like it.  But, if I have several modules
all doing some particular thing, but all needing this one (or two or three)
class, wouldn't it make sense to put it in the main module?  Hmmm perhaps
not.  Perhaps this universal class and others like it should be in a module
all by themselves.
  Either way, looks like your last advice is the route I will take.

> "Learning Python" has a nice explanation of this phenomenon under "Module
> Gotchas", and their advice is basically: "Don't do that...really!" :-).
> Redesigning helps.
>

  Haven't done Learning Python, but it can't hurt this early in development
to at least consider a redesign.

  Thanks for the advice!
  Glen.

> --
> Remco Gerlich
>