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


Groups > comp.lang.python > #81870 > unrolled thread

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

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2014-12-03 12:52 -0700
Last post2014-12-03 12:52 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-12-03 12:52 -0700
SubjectRe: Style question: Importing modules from packages - 'from' vs 'as'
Message-ID<mailman.16539.1417636348.18130.python-list@python.org>

[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".

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web