del an imported Class at EOF... why?

Hans Mulder hansmu at xs4all.nl
Wed Oct 7 14:40:11 EDT 2009


Ryan wrote:
> [....] It does beg the question for
> me. 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.

Errrhm, no.  He is not deleting the PyQt4 module from sys.modules;
he's only deleting the name QtGui from his own namespace.  Next
time Python comes across

     from PyQt4 import QtGui

, it finds that the module PyQt4 already exists in sys.modules, so
Python does not have to load the module again.  All it has to do is
bind name QtGui in the importing module to the class with the same
name in the PyQt4 module.  That does not take many cycles.


-- HansM



More information about the Python-list mailing list