del an imported Class at EOF... why?

Stephen Hansen apt.shansen at gmail.com
Wed Oct 7 08:59:47 EDT 2009


On Wed, Oct 7, 2009 at 2:31 AM, Ryan <heniser at yahoo.com> wrote:

> 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.


Python only imports modules once. The next time Python comes across that, it
looks in sys.modules, sees a copy of PyQt4, and just adds a reference to the
class in the existing already-loaded module. All 'del PyQt4' does is deletes
a reference to that module from the local namespace. It doesn't actually
delete the module or cause it to be destroyed or cleaned up, as there's
always at least one additional reference to all modules in the sys.modules
dictionary. There's no re-importing.


> In this situation, the use of __all__ is
> better. Plus, by using __all__ instead of del you do not have to worry
> about forgetting to del a class.
>

IMHO, __all__ is almost always better because of its explicitness if you
care about the API/modules/classes exposed from this module.

"del PyQt4" at the end of the file is completely harmless, though. It won't
actually affect anything.

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091007/5d505234/attachment-0001.html>


More information about the Python-list mailing list