Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The thing is, even though Python's <code>import</code> statement is designed to <em>look</em> similar to Java's, they do completely different things under the hood. As you know, in Java an <code>import</code> statement is really little more than a hint to the compiler. It basically sets up an alias for a fully qualified class name. For example, when you write</p> <pre><code>import java.util.Set; </code></pre> <p>it tells the compiler that throughout that file, when you write <code>Set</code>, you mean <code>java.util.Set</code>. And if you write <code>s.add(o)</code> where <code>s</code> is an object of type <code>Set</code>, the compiler (or rather, linker) goes out and finds the <code>add</code> method in <code>Set.class</code> and puts in a reference to it.</p> <p>But in Python,</p> <pre><code>import util.set </code></pre> <p>(that is a made-up module, by the way) does something completely different. See, in Python, packages and modules are not just <em>names</em>, they're actual <em>objects</em>, and when you write <code>util.set</code> in your code, that instructs Python to access an object named <code>util</code> and look for an attribute on it named <code>set</code>. The job of Python's <code>import</code> statement is to create that object and attribute. The way it works is that the interpreter looks for a file named <code>util/__init__.py</code>, uses the code in it to define properties of an object, and binds that object to the name <code>util</code>. Similarly, the code in <code>util/set.py</code> is used to initialize an object which is bound to <code>util.set</code>. There's a function called <a href="http://docs.python.org/library/functions.html" rel="nofollow noreferrer"><code>__import__</code></a> which takes care of all of this, and in fact the statement <code>import util.set</code> is basically equivalent to</p> <pre><code>util = __import__('util.set') </code></pre> <p>The point is, when you import a Python module, what you get is an object corresponding to the top-level package, <code>util</code>. In order to get access to <code>util.set</code> you need to go through that, and that's why it seems like you need to use fully qualified names in Python.</p> <p>There are ways to get around this, of course. Since all these things are objects, one simple approach is to just bind <code>util.set</code> to a simpler name, i.e. after the <code>import</code> statement, you can have</p> <pre><code>set = util.set </code></pre> <p>and from that point on you can just use <code>set</code> where you otherwise would have written <code>util.set</code>. (Of course this obscures the built-in <code>set</code> class, so I don't recommend actually using the name <code>set</code>.) Or, as mentioned in at least one other answer, you could write</p> <pre><code>from util import set </code></pre> <p>or</p> <pre><code>import util.set as set </code></pre> <p>This still imports the package <code>util</code> with the module <code>set</code> in it, but instead of creating a variable <code>util</code> in the current scope, it creates a variable <code>set</code> that refers to <code>util.set</code>. Behind the scenes, this works kind of like</p> <pre><code>_util = __import__('util', fromlist='set') set = _util.set del _util </code></pre> <p>in the former case, or</p> <pre><code>_util = __import__('util.set') set = _util.set del _util </code></pre> <p>in the latter (although both ways do essentially the same thing). This form is semantically more like what Java's <code>import</code> statement does: it defines an alias (<code>set</code>) to something that would ordinarily only be accessible by a fully qualified name (<code>util.set</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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