Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If Guido himself announced that the reverse domain convention ought to be followed, it wouldn't be adopted, unless there were significant changes to the implementation of <code>import</code> in python.</p> <p>Consider: python searches an import path at run-time with a fail-fast algorithm; java searches a path with an exhaustive algorithm both at compile-time and run-time. Go ahead, try arranging your directories like this:</p> <pre><code>folder_on_path/ com/ __init__.py domain1/ module.py __init__.py other_folder_on_path/ com/ __init__.py domain2/ module.py __init__.py </code></pre> <p>Then try:</p> <pre><code>from com.domain1 import module from com.domain2 import module </code></pre> <p>Exactly one of those statements will succeed. Why? Because either <code>folder_on_path</code> or <code>other_folder_on_path</code> comes higher on the search path. When python sees <code>from com.</code> it grabs the first <code>com</code> package it can. If that happens to contain <code>domain1</code>, then the first <code>import</code> will succeed; if not, it throws an <code>ImportError</code> and gives up. Why? Because <code>import</code> must occur at runtime, potentially at any point in the flow of the code (although most often at the beginning). Nobody wants an exhaustive tree-walk at that point to verify that there's no possible match. It assumes that if it finds a package named <code>com</code>, it is <em>the</em> <code>com</code> package.</p> <p>Moreover, python doesn't distinguish between the following statements:</p> <pre><code>from com import domain1 from com.domain1 import module from com.domain1.module import variable </code></pre> <p>The concept of verifying that <code>com</code> is <em>the</em> <code>com</code> is going to be different in each case. In java, you really only have to deal with the second case, and that can be accomplished by walking through the file system (I guess an advantage of naming classes and files the same). In python, if you tried to accomplish import with nothing but file system assistance, the first case could (almost) be transparently the same (<strong>init</strong>.py wouldn't run), the second case could be accomplished, but you would lose the initial running of module.py, but the third case is entirely unattainable. The code has to execute for <code>variable</code> to be available. And this is another main point: <code>import</code> does more than resolve namespaces, it executes code.</p> <p>Now, you <em>could</em> get away with this if every python package ever distributed required an installation process that searched for the <code>com</code> folder, and then the <code>domain</code>, and so on and so on, but this makes packaging considerably harder, destroys drag-and-drop capability, and makes packaging and all-out nuisance.</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