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

Terry Carroll carroll at tjc.com
Sat Aug 23 01:34:32 EDT 2003


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?

-- 
Terry Carroll        |   "I say to you that the VCR is to the American
Santa Clara, CA      |   film producer and the American public as the 
carroll at tjc.com      |   Boston strangler is to the woman home alone."  
                     |       Jack Valenti, MPAA President
Modell delendus est  |       Testimony before Congress, 1982





More information about the Tutor mailing list