del an imported Class at EOF... why?
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Wed Oct 7 18:30:58 EDT 2009
On Wed, 07 Oct 2009 02:31:00 -0700, Ryan wrote:
> Thanks everyone for your insight. I'm going to have to agree with the
> paranoid desire to prevent people importing his module and then using
> the
> classes he imports from elsewhere (I'm not ruling out the lead paint
> theory until I can gather more evidence). It does beg the question for
> me.
No it doesn't, it raises the question.
http://en.wikipedia.org/wiki/Begging_the_question
> Consider the example from his code below
>
> from PyQt4 import QtGui
>
> class LauncherWidget( QtGui.QWidget ):
> # A Specialization of QWidget
>
> del QtGui
>
> Next time python comes across
>
> from PyQt4 import QtGui
>
> it would have to re-import the class, which seems a waste of cycles that
> could accumulate.
As Hans explained, not quite. When you call "from PyQt4 import QtGui",
Python loads the module PyQt4, caches the module in sys.modules, and then
adds PyQt4.QtGui into the current namespace (your module). When you
delete QtGui, that only removes it from your namespace. The module is
still in the cache, so the next time you call the import, it's much, much
faster.
Multiple imports aren't entirely free, but they're much less expensive
than you might think.
--
Steven
More information about the Python-list
mailing list