Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81870

Re: Style question: Importing modules from packages - 'from' vs 'as'

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'importing': 0.05; 'attribute': 0.07; 'preference': 0.07; "subject:' ": 0.07; 'assigning': 0.09; 'latter': 0.09; 'os.path': 0.09; 'subject:modules': 0.09; 'subject:question': 0.10; '"from': 0.16; 'path.': 0.16; 'renaming': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; '(the': 0.22; 'import': 0.22; 'email addr:gmail.com&gt;': 0.22; 'looks': 0.24; '&gt;': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'tim': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'probably': 0.32; 'actual': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'module.': 0.36; 'doing': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'offer': 0.62; 'name': 0.63; 'more': 0.64; 'worth': 0.66; 'close': 0.67; 'qualified': 0.72; 'repeat': 0.74; 'hand': 0.80; 'dry': 0.84; 'otten': 0.84; 'yourself,': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=qFAg9pRQ8VzArkI04PhJnriremwAmsYVJ6TZ6UMEnTE=; b=G/pofy4ChgVi7RKDNooFtRyb1B2LvOtco2MX4X9dkVHbdAC0eESmbRaT78ZlOPtjsb 8RTe7Yllt3oG0E3ocFRRk+mfYrWYWMHH33LzSE6qYi20978SJ7o4hWBVx1jsZgi55Jef 62faaNt0jcc5tMvReyg4GCbb3lPqHkzD1cirpMon+mTF9U8aX+idZCgc0kCHJ5I/dxvU 9oWSbj6djujn/dNcN7Lm573WXITEDoBSZ7ckAedRdDyHAicnvzL7R+sE3Dqf5cm+w/LR Rn5AbS3bsD+HSRWc4Ve2RSJZD8/RfW3XDrh8rQPslJvxGC3uAlbB+ssshNjLZjRO5w6V Y82A==
MIME-Version 1.0
X-Received by 10.66.248.6 with SMTP id yi6mr12138868pac.120.1417636339791; Wed, 03 Dec 2014 11:52:19 -0800 (PST)
In-Reply-To <CAPTjJmqyZQ-YKmKGiZa0DJf52ENkNWRfuiJPbDjWUFnuDyutHg@mail.gmail.com>
References <CAPTjJmo1-QCgcbsjKNgH3oN-nm5qjB5GrvCiC6Qc9p+S8cNjpg@mail.gmail.com> <m5ms2d$ea0$1@ger.gmane.org> <CAPTjJmqyZQ-YKmKGiZa0DJf52ENkNWRfuiJPbDjWUFnuDyutHg@mail.gmail.com>
Date Wed, 3 Dec 2014 12:52:19 -0700
Subject Re: Style question: Importing modules from packages - 'from' vs 'as'
From Ian Kelly <ian.g.kelly@gmail.com>
To Python <python-list@python.org>
Content-Type multipart/alternative; boundary=047d7b15ac25743f890509552e54
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16539.1417636348.18130.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417636348 news.xs4all.nl 2960 [2001:888:2000:d::a6]:35201
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81870

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Dec 3, 2014 4:34 AM, "Chris Angelico" <rosuav@gmail.com> wrote:
>
> On Wed, Dec 3, 2014 at 10:27 PM, Peter Otten <__peter__@web.de> wrote:
> > Don't repeat yourself, so
> >
> > from os import path
> >
> > always. On the other hand I have never thought about actual renames, e.
g.
> >
> > from os import path as stdpath
> >
> > versus
> >
> > import os.path as stdpath
> >
> > I think I'd use the latter as it looks simpler.
>
> Thanks, Peter and Tim. Keeping DRY is worth doing (the more so as it's
> raining as I type this...), and I won't be renaming in this, so the
> from-import wins - but as Tim says, it's a close race.

To offer a counterpoint, the from import is also less explicit. With
"import os.path as path", path must be a module. With the from import, path
could be either a module or just any attribute of the os module.

My preference when importing modules is to use the fully qualified name --
os.path, not path. If I do a submodule import, I'm probably assigning a
local name anyway, so I still prefer the "import as" over the "from import
as".

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Style question: Importing modules from packages - 'from' vs 'as' Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-03 12:52 -0700

csiph-web