⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node8.html

📁 适合python初学者,一本很好的python学习书籍
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</pre></div><P>Note that when using <code>from <var>package</var> import <var>item</var></code>, the
item can be either a submodule (or subpackage) of the package, or some
other name defined in the package, like a function, class or
variable.  The <code>import</code> statement first tests whether the item is
defined in the package; if not, it assumes it is a module and attempts
to load it.  If it fails to find it, an
<tt class="exception">ImportError</tt> exception is raised.<P>需要注意的是使用 <code>from <var>package</var> import <var>item</var></code> 方式导入包时,这个子项(item)既可以是包中的一个子模块(或一个子包),也可以是包中定义的其它命名,像函数、类或变量。<code>import</code> 语句首先核对是否包中有这个子项,如果没有,它假定这是一个模块,并尝试加载它。如果没有找到它,会引发一个 <tt class="exception">ImportError</tt> 异常。<P>Contrarily, when using syntax like <code>import
<var>item.subitem.subsubitem</var></code>, each item except for the last must be
a package; the last item can be a module or a package but can't be a
class or function or variable defined in the previous item.<P>相反,使用类似<code>import <var>item.subitem.subsubitem</var></code> 这样的语法时,这些子项必须是包,最后的子项可以是包或模块,但不能是前面子项中定义的类、函数或变量。<P><H2><A NAME="SECTION008410000000000000000"></A><A NAME="pkg-import-star"></A><BR>6.4.1 以 * 方式加载包 Importing * From a Package </H2><P>Now what happens when the user writes <code>from Sound.Effects import
*</code>?  Ideally, one would hope that this somehow goes out to the
filesystem, finds which submodules are present in the package, and
imports them all.  Unfortunately, this operation does not work very
well on Mac and Windows platforms, where the filesystem does not
always have accurate information about the case of a filename!  On
these platforms, there is no guaranteed way to know whether a file
<span class="file">ECHO.PY</span> should be imported as a module <tt class="module">echo</tt>,
<tt class="module">Echo</tt> or <tt class="module">ECHO</tt>.  (For example, Windows 95 has the
annoying practice of showing all file names with a capitalized first
letter.)  The DOS 8+3 filename restriction adds another interesting
problem for long module names.<P>那么当用户写下 <code>from Sound.Effects import *</code> 时会发生什么事?理想中,总是希望在文件系统中找出包中所有的子模块,然后导入它们。不幸的是,这个操作在 Mac 和 Windows 平台上工作的并不太好,这些文件系统的文件大小写并不敏感!在这些平台上没有什么方法可以确保一个叫<span class="file">ECHO.PY</span> 的文件应该导入为模块 <tt class="module">echo</tt> 、 <tt class="module">Echo</tt> 或 <tt class="module">ECHO</tt> 。(例如,Windows 95有一个讨厌的习惯,它会把所有的文件名都显示为首字母大写的风格。)DOS 8+3文件名限制又给长文件名模块带来了另一个有趣的问题。<P>The only solution is for the package author to provide an explicit
index of the package.  The import statement uses the following
convention: if a package's <span class="file">__init__.py</span> code defines a list
named <code>__all__</code>, it is taken to be the list of module names that
should be imported when <code>from <var>package</var> import *</code> is
encountered.  It is up to the package author to keep this list
up-to-date when a new version of the package is released.  Package
authors may also decide not to support it, if they don't see a use for
importing * from their package.  For example, the file
<span class="file">Sounds/Effects/__init__.py</span> could contain the following code:<P>对于包的作者来说唯一的解决方案就是给提供一个明确的包索引。import 语句按如下条件进行转换:执行 <code>from <var>package</var> import *</code> 时,如果包中的 <span class="file">__init__.py</span> 代码定义了一个名为 <code>__all__</code> 的链表,就会按照链表中给出的模块名进行导入。新版本的包发布时作者可以任意更新这个链表。如果包作者不想 import * 的时候导入他们的包中所有模块,那么也可能会决定不支持它(import *)。例如, <span class="file">Sounds/Effects/__init__.py</span> 这个文件可能包括如下代码:<P><div class="verbatim"><pre>
__all__ = ["echo", "surround", "reverse"]</pre></div><P>This would mean that <code>from Sound.Effects import *</code> would
import the three named submodules of the <tt class="module">Sound</tt> package.<P>这意味着 <code>from Sound.Effects import *</code> 语句会从 <tt class="module">Sound</tt> 包中导入以上三个已命名的子模块。<P>If <code>__all__</code> is not defined, the statement <code>from Sound.Effects
import *</code> does <em>not</em> import all submodules from the package
<tt class="module">Sound.Effects</tt> into the current namespace; it only ensures that the
package <tt class="module">Sound.Effects</tt> has been imported (possibly running its
initialization code, <span class="file">__init__.py</span>) and then imports whatever names are
defined in the package.  This includes any names defined (and
submodules explicitly loaded) by <span class="file">__init__.py</span>.  It also includes any
submodules of the package that were explicitly loaded by previous
import statements.  Consider this code:<P>如果没有定义 <code>__all__</code> , <code>from Sound.Effects import *</code> 语句不会从 <tt class="module">Sound.Effects</tt> 包中导入所有的子模块。Effects 导入到当前的命名空间,只能确定的是导入了 Sound.Effects 包(可能会运行 <span class="file">__init__.py</span> 中的初始化代码)以及包中定义的所有命名会随之导入。这样就从 <span class="file">__init__.py</span> 中导入了每一个命名(以及明确导入的子模块)。同样也包括了前述的import语句从包中明确导入的子模块,考虑以下代码:<P><div class="verbatim"><pre>
import Sound.Effects.echo
import Sound.Effects.surround
from Sound.Effects import *</pre></div><P>In this example, the echo and surround modules are imported in the
current namespace because they are defined in the
<tt class="module">Sound.Effects</tt> package when the <code>from...import</code> statement
is executed.  (This also works when <code>__all__</code> is defined.)<P>在这个例子中,echo和surround模块导入了当前的命名空间,这是因为执行 <code>from...import</code> 语句时它们已经定义在 <tt class="module">Sound.Effects</tt> 包中了(定义了 <code>__all__</code> 时也会同样工作)。<P>Note that in general the practice of importing <code>*</code> from a module or
package is frowned upon, since it often causes poorly readable code.
However, it is okay to use it to save typing in interactive sessions,
and certain modules are designed to export only names that follow
certain patterns.<P>需要注意的是习惯上不主张从一个包或模块中用 import <code>*</code> 导入所有模块,因为这样的通常意味着可读性会很差。然而,在交互会话中这样做可以减少输入,相对来说确定的模块被设计成只导出确定的模式中命名的那一部分。<P>Remember, there is nothing wrong with using <code>from Package
import specific_submodule</code>!  In fact, this is the
recommended notation unless the importing module needs to use
submodules with the same name from different packages.<P>记住, <code>from Package import specific_submodule</code> 没有错误!事实上,除非导入的模块需要使用其它包中的同名子模块,否则这是受到推荐的写法。<P><H2><A NAME="SECTION008420000000000000000">6.4.2 内置包(Intra-package)参考 Intra-package References</A></H2><P>The submodules often need to refer to each other.  For example, the
<tt class="module">surround</tt> module might use the <tt class="module">echo</tt> module.  In fact,
such references
are so common that the <tt class="keyword">import</tt> statement first looks in the
containing package before looking in the standard module search path.
Thus, the surround module can simply use <code>import echo</code> or
<code>from echo import echofilter</code>.  If the imported module is not
found in the current package (the package of which the current module
is a submodule), the <tt class="keyword">import</tt> statement looks for a top-level
module with the given name.<P>子模块之间经常需要互相引用。例如,<tt class="module">surround</tt> 模块可能会引用 <tt class="module">echo</tt> 模块。事实上,这样的引用如此普遍,以致于 <tt class="keyword">import</tt> 语句会先搜索包内部,然后才是标准模块搜索路径。因此 surround 模块可以简单的调用 <code>import echo</code> 或者 <code>from echo import echofilter</code> 。如果没有在当前的包中发现要导入的模块,<tt class="keyword">import</tt> 语句会依据指定名寻找一个顶级模块。<P>When packages are structured into subpackages (as with the
<tt class="module">Sound</tt> package in the example), there's no shortcut to refer
to submodules of sibling packages - the full name of the subpackage
must be used.  For example, if the module
<tt class="module">Sound.Filters.vocoder</tt> needs to use the <tt class="module">echo</tt> module
in the <tt class="module">Sound.Effects</tt> package, it can use <code>from
Sound.Effects import echo</code>.<P>如果包中使用了子包结构(就像示例中的 <tt class="module">Sound</tt> 包),不存在什么从邻近的包中引用子模块的便捷方法--必须使用子包的全名。例如,如果 <tt class="module">Sound.Filters.vocoder</tt> 包需要使用 <tt class="module">Sound.Effects</tt> 包中的 <tt class="module">echosa</tt> 模块,它可以使用 <code>from Sound.Effects import echo</code> 。<P><H2><A NAME="SECTION008430000000000000000">6.4.3 多重路径中的包 Packages in Multiple Directories</A></H2><P>Packages support one more special attribute, <tt class="member">__path__</tt>.  This
is initialized to be a list containing the name of the directory
holding the package's <span class="file">__init__.py</span> before the code in that file
is executed.  This variable can be modified; doing so affects future
searches for modules and subpackages contained in the package.<P>包支持一个更为特殊的变量, <tt class="member">__path__</tt> 。 在包的 <span class="file">__init__.py</span> 文件代码执行之前,该变量初始化一个目录名列表。该变量可以修改,它作用于包中的子包和模块的搜索功能。<P>While this feature is not often needed, it can be used to extend the
set of modules found in a package.<P>这个功能可以用于扩展包中的模块集,不过它不常用。<P><BR><HR><H4>Footnotes</H4><DL><DT><A NAME="foot981">... somewhere.</A><A HREF="node8.html#tex2html7"><SUP>6.1</SUP></A></DT><DD>
        In fact function definitions are also `statements' that are
        `executed'; the execution enters the function name in the
        module's global symbol table.
</DD><DT><A NAME="foot983">...第一次导入时执行一次。</A><A HREF="node8.html#tex2html8"><SUP>6.2</SUP></A></DT><DD> 
	事实上函数定义既是“声明”又是“可执行体”;执行体由函数在模块全局语义表中的命名导入。(In fact function definitions are also `statements' that are `executed'; the execution enters the function name in the module's global symbol table. )
</DD></DL><DIV CLASS="navigation"><div class='online-navigation'><p></p><hr /><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="5. 数据结构 Data Structures"  href="node7.html"><img src='../icons/previous.png'  border='0' height='32'  alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial"  href="tut.html"><img src='../icons/up.png'  border='0' height='32'  alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="7. 输入和输出 Input and"  href="node9.html"><img src='../icons/next.png'  border='0' height='32'  alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents"  href="node2.html"><img src='../icons/contents.png'  border='0' height='32'  alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node7.html">5. 数据结构 Data Structures</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node9.html">7. 输入和输出 Input and</A></div></div><hr /></DIV><!--End of Navigation Panel--><ADDRESS>译者:刘鑫(march.liu AT gmail DOT com) 由:limodou转(limodou AT gmail DOT com)<br>See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -