[Python-Dev] Re: Allow all assignment expressions after 'import something as'

Thomas Wouters thomas@xs4all.net
Thu, 24 Aug 2000 12:18:58 +0200


On Thu, Aug 24, 2000 at 04:14:12AM -0500, Ka-Ping Yee wrote:

> The current explanation of "import <x> as <y>" is something like

>     Find and import the module named <x> and assign it to <y>
>     in the normal way you do assignment, except <y> has to be
>     a pure name.

> Thomas' suggestion lifts the restriction and makes the explanation
> simpler than it would have been:

>     Find and import the module named <x> and assign it to <y>
>     in the normal way you do assignment.

> "The normal way you do assignment" is shorthand for "decide
> whether to assign to the local or global namespace depending on
> whether <y> has been assigned to in the current scope, unless
> <y> has been declared global with a 'global' statement" -- and
> that applies in any case.  Luckily, it's a concept that has
> been explained before and which Python programmers already
> need to understand anyway.

This is not true. The *current* situation already does the local/global
namespace trick, except that 'import ..' *is* a local assignment, so the
resulting name is always local (unless there is a "global" statement.)

My patch wouldn't change that one bit. It would only expand the allowable
expressions in the 'as' clause: is it a normal name-binding assignment (like
now), or a subscription-assignment, or a slice-assignment, or an
attribute-assignment. In other words, all types of assignment.

> The net effect is essentially a direct translation to

>     <y> = __import__("<x>")

Exactly :)

> Careful:

>     import foo.bar          "import the package named foo and its
>                              submodule bar, then put *foo* into the
>                              current namespace"

Wrong. What it does is: import the package named foo and its submodule bar,
and make it so you can access foo.bar via the name 'foo.bar'. That this has
to put 'foo' in the local namespace is a side issue :-) And when seen like
that,

>     import foo.bar as spam  "import the package named foo and its
>                              submodule bar, then put *bar* into the
>                              current namespace, as spam"

Becomes obvious as well.

-- 
Thomas Wouters <thomas@xs4all.net>

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