Why import only at module level?

Peter Hansen peter at engcorp.com
Thu Feb 19 10:34:09 EST 2004


Paul Rubin wrote:
> 
> That's what the Python style guides advise.  They don't seem to like
> 
>    def frob(x):
>       import re
>       if re.search('sdf[234]xyz', x): ...
> 
> instead preferring that you pollute your module's global namespace
> with the names of all your imports.  What's the point of that?  

Maintainability.  It's also well understood that there are potential
benefits to the approach you show above, late-loading of code being
one of them (which can improve startup time for certain apps), but
as maintainability should almost always be the primary consideration,
the standard advice is to put stuff together at the top where it's
clear to maintainers which modules are used in the code (in other
words, what a given module is coupled to).

-i've-been-playing-the-esp-game-so-i-channeled-the-style-guide-authors
-Peter



More information about the Python-list mailing list