f



Use and usefulness of the as syntax

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 ?
-- The second case seems to be rather widespread and causes math 
attribute to be private but I don't figure out why this matters.
-- In the last case, I can see no point

So what is the pragmatics of the as syntax ?

Thanks for clarification.
0
candide (110)
11/12/2011 11:56:17 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

11 Replies
1198 Views

Similar Articles

[PageSpeed] 21

On Sat, Nov 12, 2011 at 10:56 PM, candide <candide@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
0
rosuav (4324)
11/12/2011 12:27:56 PM
On 12 November 2011 11:56, candide <candide@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
0
arnodel1 (164)
11/12/2011 12:29:26 PM
On 11/12/11 05:56, candide wrote:
> First, could you confirm the following syntax
>
> import foo as f
>
> equivalent to
>
> import foo
> f = foo

and the issuing "del 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 last case, I can see no point

Without context, I'm guessing the last one is merely keeping 
parity in a block that reads:

   try:
     import cPickle as pickle
   except ImportError:
     import pickle as pickle



> So what is the pragmatics of the as syntax ?

The most common use-case I see is your first:  to shorten a 
frequently-used namespace.  I do this frequently with

   import Tkinter as tk

which makes it obvious where things are coming from.  I hate 
trying to track down variable-names if one did something like

   from Tkinter import *


The second big use case I see regularly is the full example 
(above):  try to import a faster/native module that shares an 
interface with a pure-python implementation.  However in the 
above, the "import pickle as pickle" is a uselessly redundant.

-tkc


0
python.list (1515)
11/12/2011 12:43:06 PM
El 12/11/11 13:43, Tim Chase escribi�:
>   I hate trying to track down variable-names if one did something like
>
>   from Tkinter import *
>
+1
0
11/12/2011 12:48:04 PM
On Sat, 2011-11-12 at 12:56 +0100, candide wrote:

> So what is the pragmatics of the as syntax ?

Another case:

try:
    import json
except:
    import simplejson as json


(same goes for several modules where the C implementation may or may not
be available)

Tim

0
tim.wintle (99)
11/12/2011 1:03:29 PM
candide 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. [ ... ]

It gives you an out in a case like

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> os = 5      # number of 'o's
>>> import os as opsys
>>> os
5
>>> opsys
<module 'os' from '/usr/lib/python2.6/os.pyc'>

(This is an instance of what arnaud mentioned.)

	Mel.
0
mwilson (600)
11/12/2011 1:59:40 PM
On Nov 12, 7:48=A0am, Rafael Dur=E1n Casta=F1eda
<rafadurancastan...@gmail.com> wrote:
> El 12/11/11 13:43, Tim Chase escribi=F3:> =A0 I hate trying to track down=
 variable-names if one did something like
>
> > =A0 from Tkinter import *
>
> +1

Really, this questionable code is always mentioned as example in
Tkinter tuts.
IMHO much better is
>>> import Tkinter as tk
In common case `as` is useful when:
1. You `import ThirdPartyModuleWithTerriblyLongName as tpm`
2. Whant to free some common variable name, which is engaged by module
name by default
   (right what Mel Wilson has pictured)
0
11/13/2011 8:55:16 AM
On 11/13/2011 3:55 AM, 0xfn wrote:
> On Nov 12, 7:48 am, Rafael Dur=C3=A1n Casta=C3=B1eda
> <rafadurancastan...@gmail.com>  wrote:
>> El 12/11/11 13:43, Tim Chase escribi=C3=B3:>     I hate trying to trac=
k down variable-names if one did something like
>>
>>>    from Tkinter import *
>>
>> +1
>
> Really, this questionable code is always mentioned as example in
> Tkinter tuts.

I see it is still in the 3.2 tkinter doc, near the top.

> IMHO much better is
>>>> import Tkinter as tk

My opinion also. I will think about changing it someday, but then all=20
the examples will need to be changed ;-). So it will not be trivial.

--=20
Terry Jan Reedy


0
tjreedy (5460)
11/13/2011 10:16:38 PM
Thanks to all




Le 12/11/2011 13:27, Chris Angelico a �crit :
 > On Sat, Nov 12, 2011 at 10:56 PM, candide<candide@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
 >



Le 12/11/2011 13:43, Tim Chase a �crit :
 > On 11/12/11 05:56, candide wrote:
 >> First, could you confirm the following syntax
 >>
 >> import foo as f
 >>
 >> equivalent to
 >>
 >> import foo
 >> f = foo
 >
 > and the issuing "del foo"
 >




Correct, I missed this point : I didn't realize that importing is also 
binding.
0
candide (110)
11/17/2011 3:48:04 PM
Le 12/11/2011 13:29, Arnaud Delobelle a écrit :

>> -- 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 *"
>


To contextualize more, I guess you are referring to the following 
situation :



# a.py
import math as _math


# b.py
from a import *

print _math.sin(0)       # raise a NameError
print math.sin(0)        # raise a NameError



so the as syntax is also seful for hiding name, isn'it ?
0
candide (110)
11/17/2011 3:48:32 PM
On Nov 18, 1:48=A0am, candide <cand...@free.invalid> wrote:
> # a.py
> import math as _math
>
> # b.py
> from a import *
>
> print _math.sin(0) =A0 =A0 =A0 # raise a NameError
> print math.sin(0) =A0 =A0 =A0 =A0# raise a NameError
>
> so the as syntax is also seful for hiding name, isn'it ?

Not exactly. It's the * import mechanism here that's ignoring any
bindings that begin with an underscore. If you had:

   _dummy =3D 1

....inside of a.py, it wouldn't be pulled in either. As you state
later, 'as' is purely a binding convenience.

Incidentally, you can still allow * import to bring in underscore-
prefixed bindings by adding them to an __all__:

    __all__ =3D ['_dummy']
0
wuwei23 (1194)
11/18/2011 2:38:47 AM
Reply: