On Mon, Jul 20, 2009 at 7:07 AM, Pavel Skvazh<[email protected]> wrote: > def do_more(): > import os > os.do(...)
Don't do this. > 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? It has little to do with Pylons specifically, so much as it has to do with Python. Modules don't get GC'ed unless you do special tricks to unload them. After the first import, all you're effectively doing is re-establishing the namespace reference to point to the module object already stored in sys.modules. You're simply not saving memory doing this. And, in fact, you're costing yourself performance. See: http://wiki.python.org/moin/PythonSpeed/PerformanceTips#ImportStatementOverhead This type of late importing is actually costly (as well as generally ineffective), so I'd just avoid it. Your goal in importing should be to establish what you need to reference in a module that you're writing, so generally do it at the module level. Let Python take care of the rest. -- Kyle. www.kylev.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
