Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15623
| Date | 2011-11-12 06:43 -0600 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: Use and usefulness of the as syntax |
| References | <4ebe5ee6$0$25872$426a74cc@news.free.fr> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2670.1321101802.27778.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web