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


Groups > comp.lang.python > #81853

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

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'package,': 0.03; 'importing': 0.05; "subject:' ": 0.07; 'latter': 0.09; 'os.path': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:modules': 0.09; 'subject:question': 0.10; '"from"': 0.16; 'assumptions': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'two.': 0.16; '(you': 0.16; 'wrote:': 0.18; 'module': 0.19; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'convenient': 0.24; 'fairly': 0.24; 'looks': 0.24; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; "i'm": 0.30; 'code': 0.31; 'another.': 0.31; 'front': 0.32; 'quite': 0.32; 'actual': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'could': 0.34; "can't": 0.35; 'objects': 0.35; 'but': 0.35; 'throughout': 0.37; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'catch': 0.60; 'name': 0.63; 'refer': 0.63; 'side': 0.67; 'repeat': 0.74; 'hand': 0.80; 'etc,': 0.84; 'yourself,': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Style question: Importing modules from packages - 'from' vs 'as'
Date Wed, 03 Dec 2014 12:27:03 +0100
Organization None
References <CAPTjJmo1-QCgcbsjKNgH3oN-nm5qjB5GrvCiC6Qc9p+S8cNjpg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9cde.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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.16528.1417606042.18130.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417606042 news.xs4all.nl 2863 [2001:888:2000:d::a6]:56317
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81853

Show key headers only | View raw


Chris Angelico wrote:

> When importing a module from a subpackage, it's sometimes convenient
> to refer to it throughout the code with a one-part name rather than
> two. I'm going to use 'os.path' for the examples, but my actual
> use-case is a custom package where the package name is, in the
> application, quite superfluous.
> 
> Throughout the code, I want to refer to "path.split()",
> "path.isfile()", etc, without the "os." in front of them. I could do
> either of these:
> 
> import os.path as path
> from os import path
> 
> Which one would you recommend? Does it depend on context?

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.

> An "as" import works only if it's a module in a package, where the
> "from" import can also import other objects (you can't go "import
> pprint.pprint as pprint"). I'm fairly sure that's an argument... on
> one side or another. :)

In theory you could sometimes catch erroneous assumptions about 
pprint.pprint's type. But I don't care.

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


Thread

Re: Style question: Importing modules from packages - 'from' vs 'as' Peter Otten <__peter__@web.de> - 2014-12-03 12:27 +0100

csiph-web