2009/7/20 Pavel Skvazh <[email protected]> > > I was going to ask this for ages now. > > For example we've got a controller. > > import os > > class MyController(BaseController): > def do_stuff(): > import os > os.open(...) > > def do_more(): > import os > os.do(...) > > The question being, what's the best practice here for this exactly in > pylons. > When the server is launched, the top level import is done, making it > availible for everyone in the module. > If I'll only import os in the method, won't it cause more work to > import it every single time the method is run? >
They are both basically equivalent. When you do an import os, the first thing it checks is sys.modules['os'], and if it's there it just returns that module object. This is pretty cheap, and no objects are created. You might even gain the time back because now os is a local variable, and those are slightly faster than globals (if you did import at the global scope). The only time memory is effected is if you wouldn't have imported os at all. For a more obscure module this might work, though generally it just means your application "warms up" -- everything gets imported eventually, but just not all at once, trading off startup time for a little delay when you do first load the module. -- Ian Bicking | http://blog.ianbicking.org | http://topplabs.org/civichacker --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~----------~----~----~----~------~----~------~--~---
