Calling dir function on a module
<p>When I did a dir to find the list of methods in boltons I got the below output</p>

<pre><code>&gt;&gt;&gt; import boltons
&gt;&gt;&gt; dir(boltons)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
</code></pre>

<p>When I explicitly did  </p>

<pre><code>&gt;&gt;&gt; from boltons.strutils import camel2under
&gt;&gt;&gt; dir(boltons)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'strutils']
</code></pre>

<p>found that strutils getting added to attribute of boltons</p>

<p>Why is <strong>strutils</strong> not showing before explicit import?</p>

<p>From the <a href="https://docs.python.org/3/library/functions.html#dir" rel="nofollow">docs</a> on what dir does:</p>

<blockquote>
  <p>With an argument, attempt to return a list of valid attributes for
  that object.</p>
</blockquote>

<p>When we import the boltons package we can see that strutils is not an attribute on the boltons object. Therefore we do not expect it to show up in <code>dir(boltons)</code>.</p>

<pre><code>&gt;&gt;&gt;import boltons
&gt;&gt;&gt;getattr(boltons, 'strutils')
AttributeError: module 'boltons' has no attribute 'strutils'
</code></pre>

<p>The <a href="https://docs.python.org/3/reference/import.html#submodules" rel="nofollow">docs</a> on importing submodules say:</p>

<blockquote>
  <p>For example, if package spam has a submodule foo, after importing spam.foo, spam will have an attribute foo which is bound to the submodule.</p>
</blockquote>

<p>Importing a submodule creates an attribute on the package. In your example:</p>

<pre><code>&gt;&gt;&gt;import boltons
&gt;&gt;&gt;getattr(boltons, 'strutils')
AttributeError: module 'boltons' has no attribute 'strutils'
&gt;&gt;&gt;from boltons.strutils import camel2under
&gt;&gt;&gt;getattr(boltons, 'strutils')
&lt;module 'boltons.strutils' from '/usr/local/lib/python3.5/site-packages/boltons/strutils.py'&gt;
</code></pre>

<p>Therefore in this case we do expect strutils to show up in <code>dir(boltons)</code></p>

python python-2.7