'import x as y' stops the show (was: Re: [Python-Dev] [Patch #101135] 'import x as y' and 'from x import y as z' (fwd))

Thomas Wouters thomas@xs4all.net
Thu, 10 Aug 2000 08:23:16 +0200


On Thu, Aug 10, 2000 at 08:10:13AM +0000, Peter Schneider-Kamp wrote:
> Thomas Wouters wrote:

> > 'join as j' ? What would it do ? Import all symbols from 'string' into a
> > new namespace 'j' ? How about you do 'import string as j' instead ? It means
> > you will still be able to do 'j._somevar', which you probably wouldn't in
> > your example, but I don't think that's enough reason :P

> Okay, your misunderstanding of the semantics I had in mind are
> reason enough <0.5 wink>.

> from string import *, join as j
> (or equivalently)
> from string import join as j, *

Ahh, like that :) Well, I'd say 'no'. "from module import *" has only one
legitimate use, as far as I'm concerned, and that's taking over all symbols
without prejudice, to encapsulate another module. It shouldn't be used in
code that attempts to stay readable, so 'import join as j' is insanity ;-)
If you really must do the above, do it in two import statements.

> An even better way of doing this were if it would not
> only give you another name but if it would not import
> the original one. In this example our expression
> would import all the symbols from Tkinter but would
> rename createtimerhandle as tk_timer. In this way you
> could still use * if you have a namespace clash. E.g.:

No, that isn't possible. You can't pass a list of names to 'FROM_IMPORT *'
to omit loading them. (That's also the reason the patch needs a new opcode,
because you can't pass both the name to be imported from a module and the
name it should be stored at, to the FROM_IMPORT bytecode :)

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!