using del() to 'unimport'

Bengt Richter bokr at oz.net
Mon Nov 25 14:51:02 EST 2002


On Mon, 25 Nov 2002 10:43:40 -0500, "Steve Holden" <sholden at holdenweb.com> wrote:

>"TeaAndBikkie" <teaandbikkie at aol.com> wrote ...
>> >From: donnal at donnal.net  (Donnal Walter)
>> >
>> >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
>> >
>> >del(Useful)
>> >del(Mixin1)
>> >======================================
>>
>> One alternative is not to import the names in the first place, eg.
>>
>> import utilities
>> class MyClass(utilities.Useful, utilities.Mixin1): pass
>>
>> Then you would only have "utilites" in the main namespace.
>>
>
>The implication of the original post is that MyClass is being defined in its
>own module. In that case, wouldn't it make more sense to set the __all__
>attribute in the module to decide which names are imported into the client's
>namespace? From 6.12 in the Reference Manual:
>
>"""
>The public names defined by a module are determined by checking the module's
>namespace for a variable named __all__; if defined, it must be a sequence of
>strings which are names defined or imported by that module. The names given
>in __all__ are all considered public and are required to exist. If __all__
>is not defined, the set of public names includes all names found in the
>module's namespace which do not begin with an underscore character ("_").
>"""
>
>So a simple
>
>    __all__ = (MyClass, )
>
>would seem to answer here.

TypeError: attribute name must be string

     __all__ = ('MyClass', )

Regards,
Bengt Richter



More information about the Python-list mailing list