Use and usefulness of the as syntax

C

candide

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.
 
C

Chris Angelico

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
 
A

Arnaud Delobelle

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.
 
T

Tim Chase

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
 
T

Tim Wintle

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
 
M

Mel Wilson

candide said:
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.<module 'os' from '/usr/lib/python2.6/os.pyc'>

(This is an instance of what arnaud mentioned.)

Mel.
 
0

0xfn

El 12/11/11 13:43, Tim Chase escribió:>   I hate trying to track downvariable-names if one did something like


+1

Really, this questionable code is always mentioned as example in
Tkinter tuts.
IMHO much better isIn 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)
 
T

Terry Reedy

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

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

candide

Thanks to all




Le 12/11/2011 13:27, Chris Angelico a écrit :
Not quite, it's closer to:

import foo
f = foo
del foo



Le 12/11/2011 13:43, Tim Chase a écrit :
and the issuing "del foo"




Correct, I missed this point : I didn't realize that importing is also
binding.
 
C

candide

Le 12/11/2011 13:29, Arnaud Delobelle a écrit :
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 ?
 
A

alex23

# 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 ?

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

_dummy = 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__ = ['_dummy']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top