[Python-ideas] Allow 'import star' with namespaces

Eric Snow ericsnowcurrently at gmail.com
Fri May 6 22:52:09 CEST 2011


On Fri, May 6, 2011 at 2:12 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> Brendan Moloney wrote:
>
>> dag.odenhall at gmail.com wrote:
>>
>>> I like this idea, except it's inconsistent with from-import-star, the
>>> latter which does *not* get you sub-packages or modules.
>>>
>>
>> Georg Brandl [g.brandl at gmx.net] wrote:
>>
>>> And that's for a reason: it's not easy (I think it's even impossible,
>>> because
>>> for example individual submodules can change __path__) to determine all
>>> importable submodules of a package.
>>>
>>
>>  So ``import pkg.*`` would not have any behavior other than ``import
>>> pkg``.
>>>
>>
>> When I said all _public_ sub-packages and modules I was referring to those
>>
> > listed in the  __all__ attribute of 'pkg'.  Thus it would behave in the
> exact
> > same way as from-import-star except you don't pollute the current
> namespace.
>
>
> I'm not catching the vision -- could you put together a short example that
> would illustrate?
>
>
He's saying that the package would be imported like normal.  Then all
"public" sub-modules of the package would automatically imported and bound
to the namespace of the object that resulted from the import of the package.
 The trickery is that __all__ in the __init__.py would change meaning
somewhat, and, do you bind the submodules into the package's module object
or something else?

If you have a list of the submodules you want imported then you can already
accomplish this:

import parent
for mod in parent.__all_submodules__:
    __import__("parent.{}".format(mod))

Of course, this does not bind the submodules to the namespace of the package
module, but I suppose you could try that with one more step.  I am not sure
of the specific import mechanism with regards to name binding, but that
would seem to be a conflict with the way imported names for submodules are
bound.

-eric

~Ethan~
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110506/4d08b88f/attachment.html>


More information about the Python-ideas mailing list