Use and usefulness of the as syntax

Arnaud Delobelle arnodel at gmail.com
Sat Nov 12 07:29:26 EST 2011


On 12 November 2011 11:56, candide <candide at free.invalid> wrote:
> First, could you confirm the following syntax
>
> import foo as f
>
> equivalent to
>
> import foo
> f = foo
>
>
>
> Now, I was wondering about the usefulness in everyday programming of the as
> syntax within an import statement. Here are some instances retrieved from
> real code of such a syntax
>
> import numpy as np
>
> import math as _math
>
> import pickle as pickle
>
>
> -- In the first case, the syntax is motivated by brevity need, isn't it ?

Correct!

> -- The second case seems to be rather widespread and causes math attribute
> to be private but I don't figure out why this matters.

This way math doesn't get bound in the global namespace when doing
"from module import *"

> -- In the last case, I can see no point

Neither can I

> So what is the pragmatics of the as syntax ?

It can also help when you want to import two different modules with
the same name.

-- 
Arnaud



More information about the Python-list mailing list