Can someone explain __all__ in Python?
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files.  Can someone explain what this does?</p>

<p>Linked to, but not explicitly mentioned here, is exactly when <code>__all__</code> is used. It is a list of strings defining what symbols in a module will be exported when <code>from &lt;module&gt; import *</code> is used on the module.</p>

<p>For example, the following code in a <code>foo.py</code> explicitly exports the symbols <code>bar</code> and <code>baz</code>:</p>

<pre><code>__all__ = ['bar', 'baz']

waz = 5
bar = 10
def baz(): return 'baz'
</code></pre>

<p>These symbols can then be imported like so:</p>

<pre><code>from foo import *

print bar
print baz

# The following will trigger an exception, as "waz" is not exported by the module
print waz
</code></pre>

<p>If the <code>__all__</code> above is commented out, this code will then execute to completion, as the default behaviour of <code>import *</code> is to import all symbols that do not begin with an underscore, from the given namespace.</p>

<p>Reference: <a href="https://docs.python.org/3.5/tutorial/modules.html#importing-from-a-package">https://docs.python.org/3.5/tutorial/modules.html#importing-from-a-package</a></p>

<p><strong>NOTE:</strong> <code>__all__</code> affects the <code>from &lt;module&gt; import *</code> behavior only. Members that are not mentioned in <code>__all__</code> are still accessible from outside the module and can be imported with <code>from &lt;module&gt; import &lt;member&gt;</code>.</p>
