[Python-ideas] Allow multiple imports from a package while preserving its namespace
Nick Coghlan
ncoghlan at gmail.com
Fri Apr 27 07:14:52 EDT 2018
On 27 April 2018 at 01:22, Serhiy Storchaka <storchaka at gmail.com> wrote:
> I think this special cases isn't special enough to introduce a special
> syntax.
While I'm mostly inclined to agree, I do think it would be nice to
have a clean spelling for "ensure this module is fully imported, but
don't bind it locally".
Right now, the most concise spelling I can think of for that would be
to bind all the imported modules to the same throwaway variable, then
delete that variable:
import pkg
from pkg import mod1 as __, mod2 as __, mod3 as __
del __
Allowing "as None" to mean "Don't bind this name at all" would give:
import pkg
from pkg import mod1 as None, mod2 as None, mod3 as None
from . import views
from .views import base as None
It would be a bit odd though, since "None = anything" is normally a
syntax error.
Taking this idea in a completely different direction: what if we were
to take advantage of PEP 451 __spec__ attributes to enhance modules to
natively support implicit on-demand imports before they give up and
raise AttributeError? (Essentially making all submodule imports
implicitly lazy if you don't explicitly import them - you'd only be
*required* to explicitly import top level modules, with everything
under that being an implementation detail of that top level package)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list