This file is indexed.

/usr/share/doc/diveintopython-zh/html/file_handling/more_on_modules.html is in diveintopython-zh 5.4b-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>6.4.&nbsp;使用 sys.modules</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;6&nbsp;章&nbsp;异常和文件处理">
      <link rel="previous" href="for_loops.html" title="6.3.&nbsp;for 循环">
      <link rel="next" href="os_module.html" title="6.5.&nbsp;与目录共事">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航:<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">异常和文件处理</a>&nbsp;&gt;&nbsp;<span class="thispage">使用 sys.modules</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="for_loops.html" title="上一页: “for 循环”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="os_module.html" title="下一页: “与目录共事”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="fileinfo.modules"></a>6.4.&nbsp;使用 <tt class="literal"><tt class="filename">sys</tt>.modules</tt></h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p>与其它任何 <span class="application">Python</span> 的东西一样,模块也是对象。只要导入了,总可以用全局 dictionary <tt class="literal"><tt class="filename">sys</tt>.modules</tt> 来得到一个模块的引用。
            </p>
         </div>
         <div class="example"><a name="d0e15867"></a><h3 class="title">&nbsp;6.12.&nbsp;<tt class="literal"><tt class="filename">sys</tt>.modules</tt> 介绍
            </h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>import</span> sys</span>                          <a name="fileinfo.modules.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>print</span> <span class='pystring'>'\n'</span>.join(sys.modules.keys())</span> <a name="fileinfo.modules.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">win32api
os.path
os
exceptions
__main__
ntpath
nt
sys
__builtin__
site
signal
UserDict
stat</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left"><tt class="filename">sys</tt> 模块包含了系统级的信息,像正在运行的 <span class="application">Python</span> 的版本 (<tt class="literal"><tt class="filename">sys</tt>.version</tt><tt class="literal"><tt class="filename">sys</tt>.version_info</tt>),和系统级选项,像最大允许递归的深度 (<tt class="literal"><tt class="filename">sys</tt>.getrecursionlimit()</tt><tt class="literal"><tt class="filename">sys</tt>.setrecursionlimit()</tt>)。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left"><tt class="literal"><tt class="filename">sys</tt>.modules</tt> 是一个字典,它包含了从 <span class="application">Python</span> 开始运行起,被导入的所有模块。键字就是模块名,键值就是模块对象。请注意除了你的程序导入的模块外还有其它模块。<span class="application">Python</span> 在启动时预先装入了一些模块,如果你在一个 <span class="application">Python</span> <span class="acronym">IDE</span> 环境下,<tt class="literal"><tt class="filename">sys</tt>.modules</tt> 包含了你在 <span class="acronym">IDE</span> 中运行的所有程序所导入的所有模块。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>下面的例子展示了如何使用 <tt class="literal"><tt class="filename">sys</tt>.modules</tt></p>
         <div class="example"><a name="d0e15954"></a><h3 class="title">&nbsp;6.13.&nbsp;使用 <tt class="literal"><tt class="filename">sys</tt>.modules</tt></h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>import</span> fileinfo</span>         <a name="fileinfo.modules.1.3"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>print</span> <span class='pystring'>'\n'</span>.join(sys.modules.keys())</span>
<span class="computeroutput">win32api
os.path
os
fileinfo
exceptions
__main__
ntpath
nt
sys
__builtin__
site
signal
UserDict
stat</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">fileinfo</span>
<span class="computeroutput">&lt;module 'fileinfo' from 'fileinfo.pyc'&gt;</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">sys.modules[<span class='pystring'>"fileinfo"</span>]</span> <a name="fileinfo.modules.1.4"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">&lt;module 'fileinfo' from 'fileinfo.pyc'&gt;</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.1.3"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">当导入新的模块,它们加入到 <tt class="literal"><tt class="filename">sys</tt>.modules</tt> 中。这就解释了为什么第二次导入相同的模块时非常的快:<span class="application">Python</span> 已经在 <tt class="literal"><tt class="filename">sys</tt>.modules</tt> 中装入和缓冲了,所以第二次导入仅仅对字典做了一个查询。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.1.4"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">一旦给出任何以前导入过的模块名 (以字符串方式),通过 <tt class="literal"><tt class="filename">sys</tt>.modules</tt> 字典,你可以得到对模块本身的一个引用。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>下面的例子将展示通过结合使用 <tt class="literal">__module__</tt> 类属性和 <tt class="literal"><tt class="filename">sys</tt>.modules</tt> dictionary 来获取已知类所在的模块。
         </p>
         <div class="example"><a name="d0e16029"></a><h3 class="title">&nbsp;6.14.&nbsp;<tt class="literal">__module__</tt> 类属性
            </h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>from</span> fileinfo <span class='pykeyword'>import</span> MP3FileInfo</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">MP3FileInfo.__module__</span>              <a name="fileinfo.modules.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">'fileinfo'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">sys.modules[MP3FileInfo.__module__]</span> <a name="fileinfo.modules.2.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">&lt;module 'fileinfo' from 'fileinfo.pyc'&gt;</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">每个 <span class="application">Python</span> 类都拥有一个内置的<a href="../object_oriented_framework/class_attributes.html" title="5.8.&nbsp;类属性介绍">类属性</a> <tt class="literal">__module__</tt>,它定义了这个类的模块的名字。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.2.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">将它与 <tt class="literal"><tt class="filename">sys</tt>.modules</tt> 字典复合使用,你可以得到定义了某个类的模块的引用。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>现在准备好了,看看在样例程序 <a href="../object_oriented_framework/index.html">第 5 章</a> <tt class="literal"><tt class="filename">sys</tt>.modules</tt> 介绍的 <tt class="filename">fileinfo.py</tt> 中是如何使用的。这个例子显示它的一部分代码。
         </p>
         <div class="example"><a name="d0e16092"></a><h3 class="title">&nbsp;6.15.&nbsp;<tt class="filename">fileinfo.py</tt> 中的 <tt class="literal"><tt class="filename">sys</tt>.modules</tt></h3><pre class="programlisting">
    <span class='pykeyword'>def</span><span class='pyclass'> getFileInfoClass</span>(filename, module=sys.modules[FileInfo.__module__]):       <a name="fileinfo.modules.3.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
        <span class='pystring'>"get file info class from filename extension"</span>                             
        subclass = <span class='pystring'>"%sFileInfo"</span> % os.path.splitext(filename)[1].upper()[1:]        <a name="fileinfo.modules.3.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
        <span class='pykeyword'>return</span> hasattr(module, subclass) <span class='pykeyword'>and</span> getattr(module, subclass) <span class='pykeyword'>or</span> FileInfo <a name="fileinfo.modules.3.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.3.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">这是一个有两个参数的函数;<tt class="varname">filename</tt> 是必须的,但 <tt class="varname">module</tt><a href="../power_of_introspection/optional_arguments.html" title="4.2.&nbsp;使用可选参数和命名参数">可选的</a>并且 module 的缺省值包含了 <tt class="classname">FileInfo</tt> 类。这样看上去效率低,因为你可能认为 <span class="application">Python</span> 会在每次函数调用时计算这个 <tt class="literal"><tt class="filename">sys</tt>.modules</tt> 表达式。实际上,<span class="application">Python</span> 仅会对缺省表达式计算一次,是在模块导入的第一次。正如后面我们会看到的,我们永远不会用一个 <tt class="varname">module</tt> 参数来调用这个函数,所以 <tt class="varname">module</tt> 的功能是作为一个函数级别的常量。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.3.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">我们会在后面再仔细研究这一行,在我们了解了 <tt class="filename">os</tt> 模块之后。那么现在,只要相信 <tt class="varname">subclass</tt> 最终为一个类的名字就行了,像 <tt class="classname">MP3FileInfo</tt></td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.modules.3.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">你已经了解了 <a href="../power_of_introspection/getattr.html" title="4.4.&nbsp;通过 getattr 获取对象引用"><tt class="function">getattr</tt></a>,它可以通过名字得到一个对象的引用。<tt class="function">hasattr</tt> 是一个补充性的函数,用来检查一个对象是否具有一个特定的属性;在本例中,用来检查一个模块是否有一个特别的类 (然而它可以用于任何类和任何属性,就像 <tt class="function">getattr</tt>)。用英语来说,这行代码是说,“<span class="quote">If this module has the class named by <tt class="varname">subclass</tt> then return it, otherwise return the base class <tt class="classname">FileInfo</tt> (如果这个模块有一个名为 <tt class="varname">subclass</tt> 的类,那么返回它,否则返回基类 <tt class="classname">FileInfo</tt>)</span>”。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <div class="furtherreading">
            <h3>进一步阅读</h3>
            <ul>
               <li><a href="http://www.python.org/doc/current/tut/tut.html"><i class="citetitle"><span class="application">Python</span> Tutorial</i></a> 讨论了<a href="http://www.python.org/doc/current/tut/node6.html#SECTION006710000000000000000">缺省参数到底在什么时候和是如何计算的</a></li>
               <li><a href="http://www.python.org/doc/current/lib/"><i class="citetitle"><span class="application">Python</span> Library Reference</i></a> 提供了 <a href="http://www.python.org/doc/current/lib/module-sys.html"><tt class="filename">sys</tt></a> 模块的文档。
               </li>
            </ul>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="for_loops.html">&lt;&lt;&nbsp;for 循环</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#fileinfo.exception" title="6.1.&nbsp;异常处理">1</a> <span class="divider">|</span> <a href="file_objects.html" title="6.2.&nbsp;与文件对象共事">2</a> <span class="divider">|</span> <a href="for_loops.html" title="6.3.&nbsp;for 循环">3</a> <span class="divider">|</span> <span class="thispage">4</span> <span class="divider">|</span> <a href="os_module.html" title="6.5.&nbsp;与目录共事">5</a> <span class="divider">|</span> <a href="all_together.html" title="6.6.&nbsp;全部放在一起">6</a> <span class="divider">|</span> <a href="summary.html" title="6.7.&nbsp;小结">7</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="os_module.html">与目录共事&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>