__all__ as it relates to _foo, _bar, etc.

carroll at tjc.com carroll at tjc.com
Sun Aug 24 08:31:20 CEST 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 my understanding correct?

If I understand this right, then, if the coder of a module initializes
__all__, he need not worry about the _foo convention as far as
namespaces are concerned, anyway;  the __all_ overrides that for the
"from ModuleName import *"  form of import, and the importing
namespace is unaffected if using the "import ModuleName" form.


More information about the Python-list mailing list