[Python-Dev] 'import as'

Gordon McMillan gmcm@hypernet.com
Fri, 18 Aug 2000 08:35:42 -0400


Thomas Wouters wrote:
> On Thu, Aug 17, 2000 at 10:07:04PM -0400, Gordon McMillan wrote:

> > Um, maybe I'm not seeing something, but isn't the effect of
> > "import goom.bah as snarf" the same as "from goom import bah as
> > snarf"?
> 
> I don't understand what you're saying here. 'import goom.bah'
> imports goom, then bah, and the resulting module in the local
> namespace is 'goom'. That's existing behaviour (which I find
> perplexing, but I've never ran into before ;) which has changed
> in a reliable way: the local name being stored, whatever it would
> have been in a normal import, is changed into the "as-name" by
> "as <name>".

A whole lot rides on what you mean by "resulting" above. If by 
"resulting" you mean "goom", then "import goom.bah as snarf" 
would result in my namespace having "snarf" as an alias for 
"goom", and I would use "bah" as "snarf.bah". In which case 
Greg Ewing is right, and it's "import <dotted name> as ..." 
that should be outlawed, (since that's not what anyone would 
expect).

OTOH, if by "resulting" you meant "bah", things are much 
worse, because it means you must patched code you didn't 
understand ;-b.

> If you're saying that 'import goom.bah.baz as b' won't do what
> people expect, I agree. (But neither does 'import goom.bah.baz',
> I think :-)

I disagree with paranthetical comment. Maybe some don't 
understand the first time they see it, but it has precedent 
(Java), and it's the only form that works in circular imports.
 
> Maybe it's the early hour, but I really don't understand the
> problem here. Ofcourse we end up looking 'bah' in the other
> namespace, we have to import it. And I don't know what it has to
> do with circular import either ;P

"goom.bah" ends up looking in "goom" when *used*. If, in a 
circular import situation, "goom" is already being imported, an 
"import goom.bah" will succeed, even though it can't access 
"bah" in "goom". The code sees it in sys.modules, sees that 
it's being imported, and says, "Oh heck, lets keep going, it'll 
be there before it gets used".

But "from goom import bah" will fail with an import error 
because "goom" is an empty shell, so there's no way to grab 
"bah" and bind it into the local namespace.
 


- Gordon