import statement convention

Matt Nordhoff mnordhoff at mattnordhoff.com
Tue Apr 8 11:27:23 EDT 2008


MartinRinehart at gmail.com wrote:
> By convention, I've read, your module begins with its import
> statements. Is this always sensible?
> 
> I put imports that are needed for testing in the test code at the end
> of the module. If only a bit of the module has a visual interface, why
> pollute the global namespace with 'from Tkinter import *'? Wouldn't
> that be better done in a separate class or function?
> 
> Can we do a better job with a thoughtful rewrite of this convention?

I don't think anyone has been beheaded for breaking from convention when
there's a good reason... The Zen of Python does say "Although
practicality beats purity.".

(One useful related thing is the lazy import modules developed by e.g.
Bazaar and Mercurial, where you can "import foo" at the top level, but
the import won't really be done until it's actually used.)
-- 



More information about the Python-list mailing list