the demise of 'from foo import * and its implications?

Steve Holden sholden at holdenweb.com
Mon Mar 5 11:47:51 EST 2001


"Jeremy Hylton" <jeremy at alum.mit.edu> wrote in message
news:mailman.983746566.27513.python-list at python.org...
> >>>>> "RB" == Robin Becker <robin at jessikat.fsnet.co.uk> writes:
>
>   RB> my preference would be that statements legal in one context
>   RB> should be legal in another where they make sense. I cannot see
>   RB> how using import * or eval etc in nested scopes is somehow
>   RB> obviously bad or different from the same usage in non-nested
>   RB> scopes. The fact that the prohibition comes from making things
>   RB> easier for the compiler adds to my confusion.
>
> Using import * in function's is asking for trouble, because it makes
> it impossible for someone reading the code to know what a variable
> refers to.  Consider this hypothetical bit of code:
>
[ ... ]

I would go further, and remove "in functions" from the above. While it is
true that wildcard imports are more manageable at module level there is
still always the chance that a badly-designed module will stamp on your
names.

Of course, the "import module as name" variant allows us to define shorter
names to be qualified by the names from the module. This can help a lot if
you wnat to do

    import modulewithaverylongname

but don't want to have to refer to

    modulewithaverylongname.i

In that case you can just do

    import modulewithaverylongname as sh

and refer to

    sh.i

I did begin to wonder whether it might be possible to specify a name prefix,
along the lines of

    from modulewithaverylongname import * prefix sh_

to allow

    sh_i

as a reference to the module's "i", but I'm not sure that the gain is worth
the pain. Even if such a proposal were to be PEP-ified and accepted it could
not be mandated because that would break humongous amounts of existing code.

Thoughts?

regards
 Steve





More information about the Python-list mailing list