joperez@hotmail.fr wrote:
> 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)

`import ...` and `from ... import ...` does not behave in the same manner as it 
is explained in docs: 
https://docs.python.org/3/reference/simple_stmts.html#import. So they are not 
equivalent statements.

`import os.path as p` and `from os import path as p` bind the same local name 
to the same object, that is true. However, they do in a quite different manner. 
And this difference can be relevant, for instance, when are dealing with 
circular imports (ok, I cannot remember any example of this right now).

So I do not see how they are violating any principle in PEP 20 "The Zen of 
Python". Anyway, The Zen of Python is an inspirational document, not a law. 
Even it it was the law, any law has its exceptions and PEP 221 "Import As" 
presents and explains one useful exception. In my opinion...

Thank you.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MGLG5YJMDLDU3LHCJP2POCVFW33342LB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to