Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15620 > unrolled thread
| Started by | candide <candide@free.invalid> |
|---|---|
| First post | 2011-11-12 12:56 +0100 |
| Last post | 2011-11-17 16:48 +0100 |
| Articles | 12 — 10 participants |
Back to article view | Back to comp.lang.python
Use and usefulness of the as syntax candide <candide@free.invalid> - 2011-11-12 12:56 +0100
Re: Use and usefulness of the as syntax Chris Angelico <rosuav@gmail.com> - 2011-11-12 23:27 +1100
Re: Use and usefulness of the as syntax Arnaud Delobelle <arnodel@gmail.com> - 2011-11-12 12:29 +0000
Re: Use and usefulness of the as syntax candide <candide@free.invalid> - 2011-11-17 16:48 +0100
Re: Use and usefulness of the as syntax alex23 <wuwei23@gmail.com> - 2011-11-17 18:38 -0800
Re: Use and usefulness of the as syntax Tim Chase <python.list@tim.thechases.com> - 2011-11-12 06:43 -0600
Re: Use and usefulness of the as syntax Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-11-12 13:48 +0100
Re: Use and usefulness of the as syntax 0xfn <oleg.rimko@gmail.com> - 2011-11-13 00:55 -0800
Re: Use and usefulness of the as syntax Terry Reedy <tjreedy@udel.edu> - 2011-11-13 17:16 -0500
Re: Use and usefulness of the as syntax Tim Wintle <tim.wintle@teamrubber.com> - 2011-11-12 13:03 +0000
Re: Use and usefulness of the as syntax Mel Wilson <mwilson@the-wire.com> - 2011-11-12 08:59 -0500
Re: Use and usefulness of the as syntax candide <candide@free.invalid> - 2011-11-17 16:48 +0100
| From | candide <candide@free.invalid> |
|---|---|
| Date | 2011-11-12 12:56 +0100 |
| Subject | Use and usefulness of the as syntax |
| Message-ID | <4ebe5ee6$0$25872$426a74cc@news.free.fr> |
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.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-11-12 23:27 +1100 |
| Message-ID | <mailman.2668.1321100879.27778.python-list@python.org> |
| In reply to | #15620 |
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
[toc] | [prev] | [next] | [standalone]
| From | Arnaud Delobelle <arnodel@gmail.com> |
|---|---|
| Date | 2011-11-12 12:29 +0000 |
| Message-ID | <mailman.2669.1321100968.27778.python-list@python.org> |
| In reply to | #15620 |
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
[toc] | [prev] | [next] | [standalone]
| From | candide <candide@free.invalid> |
|---|---|
| Date | 2011-11-17 16:48 +0100 |
| Message-ID | <4ec52cd3$0$16583$426a74cc@news.free.fr> |
| In reply to | #15622 |
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 ?
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2011-11-17 18:38 -0800 |
| Message-ID | <1772de52-5ce0-4cb0-ac3c-e43aa2d17cf6@a5g2000vbb.googlegroups.com> |
| In reply to | #15822 |
On Nov 18, 1:48 am, candide <cand...@free.invalid> wrote:
> # 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']
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2011-11-12 06:43 -0600 |
| Message-ID | <mailman.2670.1321101802.27778.python-list@python.org> |
| In reply to | #15620 |
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
[toc] | [prev] | [next] | [standalone]
| From | Rafael Durán Castañeda <rafadurancastaneda@gmail.com> |
|---|---|
| Date | 2011-11-12 13:48 +0100 |
| Message-ID | <mailman.2671.1321102090.27778.python-list@python.org> |
| In reply to | #15620 |
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
[toc] | [prev] | [next] | [standalone]
| From | 0xfn <oleg.rimko@gmail.com> |
|---|---|
| Date | 2011-11-13 00:55 -0800 |
| Message-ID | <54b0617a-3456-4878-819c-f79b388232ea@u6g2000vbg.googlegroups.com> |
| In reply to | #15624 |
On Nov 12, 7:48 am, Rafael Durán Castañeda <rafadurancastan...@gmail.com> wrote: > 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 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)
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-11-13 17:16 -0500 |
| Message-ID | <mailman.2686.1321222611.27778.python-list@python.org> |
| In reply to | #15636 |
On 11/13/2011 3:55 AM, 0xfn wrote: > On Nov 12, 7:48 am, Rafael Durán Castañeda > <rafadurancastan...@gmail.com> wrote: >> 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 > > 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 the examples will need to be changed ;-). So it will not be trivial. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Tim Wintle <tim.wintle@teamrubber.com> |
|---|---|
| Date | 2011-11-12 13:03 +0000 |
| Message-ID | <mailman.2672.1321103019.27778.python-list@python.org> |
| In reply to | #15620 |
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
[toc] | [prev] | [next] | [standalone]
| From | Mel Wilson <mwilson@the-wire.com> |
|---|---|
| Date | 2011-11-12 08:59 -0500 |
| Message-ID | <j9lu4p$qqa$1@speranza.aioe.org> |
| In reply to | #15620 |
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.
[toc] | [prev] | [next] | [standalone]
| From | candide <candide@free.invalid> |
|---|---|
| Date | 2011-11-17 16:48 +0100 |
| Message-ID | <4ec52cb7$0$16583$426a74cc@news.free.fr> |
| In reply to | #15620 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web