Brad Tilley <[EMAIL PROTECTED]> writes: > When memory usage is a concern, is it better to do: > > from X import Y > > or > > import X
Depending on "Y", the latter can technically use less memory, but it's likely to be fairly small and depends on how many symbols from that module you want to have local bindings. In general, it's not worth worrying about. For example, if module X has 1000 bound names in it, then doing an "import X" loads the module and gives your current namespace a single reference to the entire module. If however, you do a "from X import *" you still load the X module, but also get local names bound for each of the names in the X module. So you have double the references to the objects created within X. Those references take up space (for the textual name and the pointer) in your current namespace's dictionary. But if you just do a "from X import Y" where Y is a single symbol, then it's a wash because you get Y bound as a local namespace name, but you don't have a reference to the module itself. If you were to import multiple symbols, then you'd have a few extra references locally. Sometimes though it's not even the memory you can affect, but performance. One place where I did find this to be noticeable, for example, was with wxPython, where older releases typically often did a "from wxPython.wx import *" since all the wxPython names already had a "wx" prefix. But that yielded thousands of extra name bindings in the local namespace. Switching to "from wxPython import wx" and then using "wx.XXX" instead of just "XXX" actually made a fairly dramatic decrease in load time. It did also drop memory because I had a bunch of plugin modules, all of which were burning up a few thousand name bindings for the same wxPython symbols. Switching them to just use the module reference was a noticeable savings in that case. > Also, is there a way to load and unload modules as they are needed. I > have some scripts that sleep for extended periods during a while loop > and I need to be as memory friendly as possible. I can post a detailed > script that currently uses ~ 10MB of memory if anyone is interested. You can play tricks by manually deleting a module out of sys.modules, but there's no guarantee that the memory will actually be released (either by Python or the OS). Unless you're working under very specific external resources, I'd generally leave this to the OS. It will figure out when some of your working set is unused for extended periods and generally it should end up in swap space if you actually need the memory for something else. -- David -- http://mail.python.org/mailman/listinfo/python-list