[Import-SIG] Dabeaz's weird import discovery

Ethan Furman ethan at stoneleaf.us
Wed Apr 22 18:21:53 CEST 2015


On 04/22, Barry Warsaw wrote:

> Poking around in Lib/importlib/_bootstrap.py, I think you can see where this
> happens.  In _find_and_load_unlocked(), 'round about line 2224 (in 3.5's
> hg:95593), you see this:
> 
>     if parent:
>         # Set the module as an attribute on its parent.
>         parent_module = sys.modules[parent]
>         setattr(parent_module, name.rpartition('.')[2], module)
> 
> It's clearly intentional, and fundamental to importlib so I don't think it's
> dependent on finder or loader.  No matter how it happens, if a submodule is
> imported, its parent namespace gets a name binding to the submodule.

I see that it's intentional, but why is it fundamental?
 
> What was the motivation for this?  Was the intent really to bind submodule
> names in the parent module seemingly magically?

I have to say I don't care for this.  In the cases where I'm importing sub-
modules into __init__ I'm usually trying to keep a neat namespace, and with
this behavior I'll have more work to do... although probably not a big deal
with __all__ in the picture, so I could live with it if it is indeed
"fundamental".  ;)

--
~Ethan~


More information about the Import-SIG mailing list