import statement within a function

Tomasz Lisowski lisowski.tomasz at sssa.NOSPAM.com.pl
Fri Feb 2 07:34:15 EST 2001


U¿ytkownik "Fredrik Lundh" <fredrik at effbot.org> napisa³ w wiadomo¶ci
news:rgve6.11079$AH6.1774088 at newsc.telia.net...
> Tomasz Lisowski wrote:
> > OK, I have read your mini-guide, and the language reference, and now I
see,
> > that the penalty for executing import statement after it has been
imported
> > for the first time is minimal. What are then advantages of importing a
> > module WITHIN a function.
>
> - lazy importing: if you place it within a function, the module
> is only imported if your program really needs it.
>
> - import dependencies (see the recursive import section in the
> mini-guide).  if you use from-import on the module level, you
> can end up in situations where the things you import doesn't
> yet exist.
>
> - performance: local lookup is faster than global lookup.

 Yes, that's really a good point!

> - readability: it can be easier to grok your code if you import
> things (i.e. define names) near the place you're using them.

Thanks, Frederik, that is really a good explanation! BTW, I really
appreciate your "Introduction to Tkinter". I am using it alot.

Tomasz





More information about the Python-list mailing list