[Tutor] __all__ cf. _foo, _bar, etc.

Lloyd Kvam pythontutor at venix.com
Wed Aug 27 13:02:14 EDT 2003


The documentation matches what you've written.

You can test things using the dir function.  If you start a python
session and type dir() you will get a list of the names defined in
your namespace.  This should be something along the lines of:
['__builtins__', '__doc__', '__name__']

Now do:
from ModuleName import *
and then type:
dir()
You will get the names that have been added to your namespace along with
the original names.  This should allow you to verify that import is
loading the names that you expect.

Terry Carroll wrote:

> I'm trying to get straight about the difference and interaction between 
> the __all__ variable and the prefixing a variable name with a single 
> underscore (e.g., _foo, _bar).
> 
> My understanding is that if you code:
> 
>  from ModuleName import *
> 
> if __all__ is initialized in the module, all of the names listed in that 
> list are made available; if __all_ is not initialized, every name not 
> beginning with an underscore is made available.
> 
> If you code:
> 
>  import ModuleName
> 
> Neither the __all__ or the underscore-prefixed names make any difference, 
> because the names from the module are not made available except through an 
> explicit reference to them (e.g., ModuleName.spam), regardless of whether 
> __all__ was initialized or whether the variable is prefixed with an 
> underscore (i.e., you can reference ModuleName._spam if you wish, but of 
> course do so at your own risk).
> 
> Is this correct?
> 
> If I understand this right, then, if the coder of a module initializes
> __all__, he need not worry about the _foo convention;  the __all_
> overrides that for the "from ModuleName import *"  form of import, and the
> importing namespace is unaffected if using the "import ModuleName" form.
> 
> Right?
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582




More information about the Tutor mailing list