Where to import modules?
newsgroups at jhrothjr.com
Fri Sep 26 16:05:09 CEST 2003
"JCM" <joshway_without_spam at myway.com> wrote in message
news:bl1f7t$oig$1 at fred.mathworks.com...
> Peter Hansen <peter at engcorp.com> wrote:
> > Basically doing this is an optimization, and you shouldn't be optimizing
> > before the program is working, and you shouldn't be optimizing without
> > a real need for it. (That is, just because it's a little faster doesn't
> > mean it's worth doing.)
> Aside from being an optimization, it's also good for documentation.
> If you see an import at the top of a file it might not be immediately
> clear how or why the module is being used. It's similar reasoning to
> why you might define variables in the narrowest scope posible.
There are also reasons having to do with circular imports that
you might not want to import at the top. However, this situation
is hazardous enough that some effort in removing the circularity
is usually warranted.
In general, I agree with the style guide: put them at the top
of the module unless there is a real, strong reason to put
them somewhere else. Even then, I'd put a comment in
with the main imports pointing out where the embedded
More information about the Python-list