__all__

Paul Woolcock pwoolcoc at gmail.com
Tue Aug 9 17:42:28 EDT 2011


The gurus will have to correct me if this is not an accepted practice, but I
know some projects (Fabric is the one that comes to mind) will define a
submodule specifically for the 'from blah import *' situation. The submodule
would be called "api", or something like that, so you can do: 'from
mymodule.api import *' to separate the items you want to include in your
public api, while still keeping other names importable by 'import blah'
On Aug 9, 2011 1:00 PM, "Steven D'Aprano" <
steve+comp.lang.python at pearwood.info> wrote:
> Ethan Furman wrote:
>
>> Greetings!
>>
>> Does anyone know/recall the original purpose of __all__?
>
> To customise the names available for `from ... import *`:
>
> http://docs.python.org/whatsnew/2.1.html#other-changes-and-fixes
>
>
>> I had thought it was primarily to specify what would be imported when
>> `from ... import *` was executed, such as for tk;
>
> Yes, that was the original use. If __all__ is not defined, Python will
> import everything that doesn't start with an underscore.
>
>
>> today, it seems it is
>> also used to specify the API for the module, and so the help() subsystem
>> will only provide details for those items found in __all__.
>
> The two meanings are assumed to be synonymous: names in the public API
> should be importable with *, and importable names should be in the public
> API. You can't specify "this can be imported with *, but isn't public"
> or "this is public, but not importable with *".
>
> http://docs.python.org/reference/simple_stmts.html#index-1090
>
>
> Also, the behaviour of __all__ with packages may be slightly different:
>
> http://docs.python.org/tutorial/modules.html#index-1134
>
>
>> The issue I'm having with this is that there are roughly a dozen items I
>> would like to make available via the `import *` mechanism in my dbf
>> module, and another dozen that, while part of the public API, don't need
>> to be available via an `import *`.
>
> You have to choose both, or neither, but you can't have just one.
>
>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110809/63d07986/attachment-0001.html>


More information about the Python-list mailing list