Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81871 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2014-12-03 15:13 -0500 |
| Last post | 2014-12-03 15:13 -0500 |
| 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.
Re: Style question: Importing modules from packages - 'from' vs 'as' Terry Reedy <tjreedy@udel.edu> - 2014-12-03 15:13 -0500
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-12-03 15:13 -0500 |
| Subject | Re: Style question: Importing modules from packages - 'from' vs 'as' |
| Message-ID | <mailman.16540.1417637615.18130.python-list@python.org> |
On 12/3/2014 6:02 AM, 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? I confirmed that they do the same thing for submodules. >>> import os.path as pth >>> from os import path >>> pth <module 'ntpath' from 'C:\\Programs\\Python34\\lib\\ntpath.py'> >>> path <module 'ntpath' from 'C:\\Programs\\Python34\\lib\\ntpath.py'> >>> id(pth) 4319096 >>> id(path) 4319096 I and most code I have seen uses "from tkinter import ttk". > 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. "import tkinter.ttk as ttk" makes it clear that ttk is a module rather than, say, a class. That might make things easier for the reader. On the other hand, duplication implies that there might be a real renaming, so having to compare to see that there is not, is extra work. from idlelib import EditorWindow import idlelib.EditorWindow as EditorWindow -- Terry Jan Reedy
Back to top | Article view | comp.lang.python
csiph-web