Best practices with import?

Chris Barker chrishbarker at home.net
Thu Aug 2 20:53:57 CEST 2001


Jay O'Connor wrote:

> *THAT* would be something useful to
> have...a class level import; import the module in __init__, it's in
> scope for the whole class
> 
> The best workaround for now I could think of is either to assign an
> instance variable to the module
> 
> class MyEngine:
>         def __init__ (self):
>                 import engine_support
>                 self._es = engine_support
> 
>         def doSomething(self):
>                 self._es.do_something()
> 
> That works...I tested it...

That's a good way to do it.
 
> or to assign it to a global in the module so that once it's imported,
> it's available to the whole module.

That's not so good.

another option is:

class MyEngine:
    import engine_support

    def __init__(self):
        pass
    def doSomething(slef):
        self.engine_support.function() 

Not really that different, all you are doing is saving the import in all
the __init__s, but as has been pointed out, they are not very expensive.

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list