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

Guido van Rossum guido at python.org
Sat May 7 16:41:55 CEST 2011


On Sat, May 7, 2011 at 1:58 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 7 May 2011 01:38, Guido van Rossum <guido at python.org> wrote:
>> The point is that the pkg should use __all__ to declare what submodules
>> exist. That's what it was invented for!
>
> Hmm, OK. I missed that. But how would that work?
>
> p1/__init__.py:
>
> __all__ = ['p2', 'foo']
> def foo(): print "p1.foo"
>
> p1/p2/__init__.py:
>
> __all__ = ['foo']
> def foo(): print "p1.foo"
>
> If I import p1, p1.__all__ shows me that p2 and foo are public. p1.foo
> exists and I can tell it's not a module. p1.p2 doesn't exist in the p1
> namespace at the moment, so how do I tell that I need to import it?
> Just assume all nonexistent names are subpackages, and import them?
> That doesn't seem like a very robust approach.

Do whatever "from pkg import *" does today.

Though the recursive application is new. I think (if we do this) it
should be recursive. The implementation is straightforward, though the
consequences may not be (think cyclic imports).

> A proof of concept in the form of a Python implementation (as a
> function) would help me understand, I guess. (But I still doubt that
> even if it's implementable, the feature is much practical use...)

It deviates from "import what you use" for sure. OTOH it is a better
alternative to "from pkg import *" because it does not pollute the
namespace. I believe Java users are used to this.

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list