On Fri, Oct 07, 2005, Kent Johnson wrote:
>Pierre Barbier de Reuille wrote:
>> (never import
>> a module in a small function likely to be called in an inner-loop !)
>
>That's good advice, but I would say "in a *time-critical* inner-loop". After 
>the first import, importing a module is fast, it is just a few dictionary 
>lookups (looking up the module in sys.modules) and a dictionary write (to the 
>local namespace). It's only the first import that is potentially expensive.

Another way that I use this is to handle parts of a module that may not
always be used (e.g. curses routines for interactive use).

In this case we have a module that provides our curses interactive screen
routines, and it has something like this:

import os
os.environ['cursesstuff'] = True

Then in the more general purpose routines:

# stuff goes here that's not interactive

if os.environ.get('cursestuff']:
        import ...
        class ...

# other things that don't need the extra functions.

All that's necessary to activate the interactive classes and other related
stuff is to include the curses routines before importing the modules that
may or may not use them.

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

``It wasn't raining when Noah built the ark.''
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to