Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The "advantage" of <code>from xyz import *</code> as opposed to other forms of import is that it imports <em>everything</em> (well, almost... [see (a) below] everything) from the designated module under the current module. This allows using the various objects (variables, classes, methods...) from the imported module <strong>without prefixing them with the module's name</strong>. For example</p> <pre><code>&gt;&gt;&gt; from math import * &gt;&gt;&gt;pi 3.141592653589793 &gt;&gt;&gt;sin(pi/2) &gt;&gt;&gt;1.0 </code></pre> <p><strong>This practice (of importing * into the current namespace) is however discouraged</strong> because it</p> <ul> <li>provides the opportunity for namespace collisions (say if you had a variable name pi prior to the import)</li> <li>may be inefficient, if the number of objects imported is big</li> <li>doesn't explicitly document the origin of the variable/method/class (it is nice to have this "self documentation" of the program for future visit into the code)</li> </ul> <p>Typically we therefore limit this import * practice to ad-hoc tests and the like. As pointed out by @Denilson-Sá-Maia, some libraries such as (e.g. pygame) have a sub-module where all the most commonly used constants and functions are defined and such sub-modules are effectively <em>designed</em> to be imported with <code>import *</code>. Other than with these special sub-modules, it is otherwise preferable to ...:</p> <p><strong>explicitly import a few objects only</strong></p> <pre><code>&gt;&gt;&gt;from math import pi &gt;&gt;&gt;pi &gt;&gt;&gt;3.141592653589793 &gt;&gt;&gt; sin(pi/2) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; NameError: name 'sin' is not defined </code></pre> <p>or <strong>import the module under its own namespace</strong> (or an alias thereof, in particular if this is a long name, and the program references its objects many times)</p> <pre><code> &gt;&gt;&gt;import math &gt;&gt;&gt;math.pi &gt;&gt;&gt;3.141592653589793 etc.. &gt;&gt;&gt;import math as m #bad example math being so short and standard... &gt;&gt;&gt;m.pi &gt;&gt;&gt;3.141592653589793 etc.. </code></pre> <p>See the <a href="http://docs.python.org/reference/simple_stmts.html#the-import-statement" rel="nofollow noreferrer"><strong>Python documentation on this topic</strong></a></p> <p><strong>(a) Specifically, what gets imported with <code>from xyz import *</code> ?</strong> <br> if xyz module defines an <code>__all__</code> variable, it will import all the names defined in this sequence, otherwise it will import all names, except these which start with an underscore.</p> <p><strong>Note</strong> Many libraries have <strong><em>sub-modules</em></strong>. For example the standard library <code>urllib</code> includes sub-modules like <code>urllib.request</code>, <code>urllib.errors</code>, <code>urllib.response</code> etc. A common point of confusion is that</p> <p><code>from urllib import *</code></p> <p>would import all these sub-modules. <strong>That is NOT the case</strong>: one needs to explicitly imports these separately with, say, <code>from urllib.request import *</code> etc. This incidentally is not specific to <code>import *</code>, plain <code>import</code> will not import sub-modules either (but of course, the <code>*</code> which is often a shorthand for <em>"everything"</em> may mislead people in thinking that all sub-modules and everything else would be imported). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload