Use and usefulness of the as syntax

Chris Angelico rosuav at gmail.com
Sat Nov 12 07:27:56 EST 2011


On Sat, Nov 12, 2011 at 10:56 PM, candide <candide at free.invalid> wrote:
> import foo as f
>
> equivalent to
>
> import foo
> f = foo
>

Not quite, it's closer to:

import foo
f = foo
del foo

without the fiddling around. What the 'import... as' syntax gives is a
way to separate the thing loaded from the name bound to. Suppose
importing were done thus:

foo = import("foo.py")

Then you'd have a standard convention of always importing into a
variable of the same name (loose terminology, but you know what I
mean), with the option of importing as something completely different.
The "import... as" syntax gives the same power, without forcing you to
repeat yourself in the common situation.

ChrisA



More information about the Python-list mailing list