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 import
in python.
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:
folder_on_path/
com/
__init__.py
domain1/
module.py
__init__.py
other_folder_on_path/
com/
__init__.py
domain2/
module.py
__init__.py
Then try:
from com.domain1 import module
from com.domain2 import module
Exactly one of those statements will succeed. Why? Because either folder_on_path
or other_folder_on_path
comes higher on the search path. When python sees from com.
it grabs the first com
package it can. If that happens to contain domain1
, then the first import
will succeed; if not, it throws an ImportError
and gives up. Why? Because import
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 com
, it is the com
package.
Moreover, python doesn't distinguish between the following statements:
from com import domain1
from com.domain1 import module
from com.domain1.module import variable
The concept of verifying that com
is the com
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 (init.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 variable
to be available. And this is another main point: import
does more than resolve namespaces, it executes code.
Now, you could get away with this if every python package ever distributed required an installation process that searched for the com
folder, and then the domain
, and so on and so on, but this makes packaging considerably harder, destroys drag-and-drop capability, and makes packaging and all-out nuisance.