
There should be one-- and preferably only one --obvious way to do it. (from The Zen of Python https://www.python.org/dev/peps/pep-0020/) However, in something as basic as import syntax, that's not the case. This example comes from PEP 221 (https://www.python.org/dev/peps/pep-0221/) : A slightly special case exists for importing sub-modules. The statement `import os.path` stores the module os locally as os, so that the imported submodule path is accessible as os.path. As a result, `import os.path as p` stores os.path, not os, in p. This makes it effectively the same as `from os import path as p` Not only it doesn't respect the Zen of Python, but it's also quite counterintuitive because as explained in the PEP, the behavior of `import os.path as p` is not the same than `import os.path`, while `from os import path as p` is quite consistent with or without `as`. There is one case where `import ... as ...` is consistent (and justified IMHO), that's for statements like `import _thread as thread`, only the imported object is aliased (as `from ... import ... as ...` do). Looking at the standard library, only few dozens of lines match the regex `^import \w+\.(\w|\.)+ as`, while the other (equivalent) form has hundreds of matches. That's why I propose to restrict the aliased import statement (`import ... as ...`) to not be able to alias imported submodule, letting `from ... import ... as ...` statement be the only to do it. The roadmap could be to depreciate the statement with a warning in a few next releases, to remove finally remove the syntax. (hoping my English is okay)
participants (1)
-
joperez@hotmail.fr