using del() to 'unimport'

Terry Reedy tjreedy at udel.edu
Sun Nov 24 15:20:28 EST 2002


"Donnal Walter" <donnal at donnal.net> wrote in message
news:918bc22f.0211240425.68b8c282 at posting.google.com...
> When the the following module is imported, I want only 'MyClass'
(NOT
> 'Useful' and 'Mixin1') to be publicly accessible.
>
> ======= mymodule.py ==================
> from utilities import Useful, Mixin1
>
> class MyClass(Useful, Mixin1): pass
> ======================================
>
>
> So my question is this: Is it an acceptable Python idiom to use
del()
> at the end of a module to remove previously imported names from the
> global namespace as shown below? Is there a better alternative?
>
> ======= mymodule.py ==================
> from utilities import Useful, Mixin1
>
> class MyClass(Useful, Mixin1): pass
>
> del(Useful)
> del(Mixin1)
> ======================================

I should think it a good idea to clean the module namespace of
everything not needed to be kept or exported.  Something that needs to
be kept but not exported should be given an _xxx name to block export.
For imported modules, use 'as', as in
from utilities import Useful as _Useful

You can instead use __all__ to control name export (see RefMan 6.12).

Terry J. Reedy






More information about the Python-list mailing list