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

📄 node8.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<P><H1><A NAME="SECTION008300000000000000000"></A><A NAME="dir"></A><BR>6.3 <tt class="function">dir()</tt> 函数 <tt class="function">dir()</tt> Function </H1><P>The built-in function <tt class="function">dir()</tt> is used to find out which names
a module defines.  It returns a sorted list of strings:<P>内置函数 <tt class="function">dir()</tt>
用于按模块名搜索模块定义,它返回一个字符串类型的存储列表:<P><div class="verbatim"><pre>
&gt;&gt;&gt; import fibo, sys
&gt;&gt;&gt; dir(fibo)
['__name__', 'fib', 'fib2']
&gt;&gt;&gt; dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
 '__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
 'builtin_module_names', 'byteorder', 'callstats', 'copyright',
 'displayhook', 'exc_clear', 'exc_info', 'exc_type', 'excepthook',
 'exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getdlopenflags',
 'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache',
 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout',
 'version', 'version_info', 'warnoptions']</pre></div><P>Without arguments, <tt class="function">dir()</tt> lists the names you have defined
currently:<P>无参数调用时, <tt class="function">dir()</tt> 函数返回当前定义的命名:<P><div class="verbatim"><pre>
&gt;&gt;&gt; a = [1, 2, 3, 4, 5]
&gt;&gt;&gt; import fibo, sys
&gt;&gt;&gt; fib = fibo.fib
&gt;&gt;&gt; dir()
['__name__', 'a', 'fib', 'fibo', 'sys']</pre></div><P>Note that it lists all types of names: variables, modules, functions, etc.<P>该列表列出了所有类型的名称:变量,模块,函数,等等:<P><tt class="function">dir()</tt> does not list the names of built-in functions and
variables.  If you want a list of those, they are defined in the
standard module <tt class="module">__builtin__</tt><a id='l2h-41' xml:id='l2h-41'></a>:<P><tt class="function">dir()</tt> 不会列出内置函数和变量名。如果你想列出这些内容,它们在标准模块 <tt class="module">__builtin__</tt><a id='l2h-42' xml:id='l2h-42'></a>中定义:<P><div class="verbatim"><pre>
&gt;&gt;&gt; import __builtin__
&gt;&gt;&gt; dir(__builtin__)
['ArithmeticError', 'AssertionError', 'AttributeError',
 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError',
 'Exception', 'False', 'FloatingPointError', 'IOError', 'ImportError',
 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
 'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning',
 'PendingDeprecationWarning', 'ReferenceError',
 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
 'True', 'TypeError', 'UnboundLocalError', 'UnicodeError', 'UserWarning',
 'ValueError', 'Warning', 'ZeroDivisionError', '__debug__', '__doc__',
 '__import__', '__name__', 'abs', 'apply', 'bool', 'buffer',
 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex',
 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod',
 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float',
 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id',
 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter',
 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min',
 'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit',
 'range', 'raw_input', 'reduce', 'reload', 'repr', 'round',
 'setattr', 'slice', 'staticmethod', 'str', 'string', 'sum', 'super',
 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']</pre></div><P><H1><A NAME="SECTION008400000000000000000"></A><A NAME="packages"></A><BR>6.4 包 Packages </H1><P>Packages are a way of structuring Python's module namespace
by using ``dotted module names''.  For example, the module name
<tt class="module">A.B</tt> designates a submodule named "<tt class="samp">B</tt>" in a package named
"<tt class="samp">A</tt>".  Just like the use of modules saves the authors of different
modules from having to worry about each other's global variable names,
the use of dotted module names saves the authors of multi-module
packages like NumPy or the Python Imaging Library from having to worry
about each other's module names.<P>包通常是使用用“圆点模块名”的结构化模块命名空间。例如,名为 <tt class="module">A.B</tt> 的模块表示了名为 "<tt class="samp">B</tt>" 的包中名为 "<tt class="samp">A</tt>" 的子模块。正如同用模块来保存不同的模块架构可以避免全局变量之间的相互冲突,使用圆点模块名保存像 NumPy 或 Python Imaging Library 之类的不同类库架构可以避免模块之间的命名冲突。<P>Suppose you want to design a collection of modules (a ``package'') for
the uniform handling of sound files and sound data.  There are many
different sound file formats (usually recognized by their extension,
for example: <span class="file">.wav</span>, <span class="file">.aiff</span>, <span class="file">.au</span>), so you may need
to create and maintain a growing collection of modules for the
conversion between the various file formats.  There are also many
different operations you might want to perform on sound data (such as
mixing, adding echo, applying an equalizer function, creating an
artificial stereo effect), so in addition you will be writing a
never-ending stream of modules to perform these operations.  Here's a
possible structure for your package (expressed in terms of a
hierarchical filesystem):<P>假设你现在想要设计一个模块集(一个“包”)来统一处理声音文件和声音数据。存在几种不同的声音格式(通常由它们的扩展名来标识,例如:<span class="file">.wav</span> , <span class="file">.aiff</span> , <span class="file">.au</span>) ),于是,为了在不同类型的文件格式之间转换,你需要维护一个不断增长的包集合。可能你还想要对声音数据做很多不同的操作(例如混音,添加回声,应用平衡功能,创建一个人造效果),所以你要加入一个无限流模块来执行这些操作。你的包可能会是这个样子(通过分级的文件体系来进行分组):<P><div class="verbatim"><pre>
Sound/                          Top-level package
      __init__.py               Initialize the sound package
      Formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      Effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      Filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...</pre></div><P>When importing the package, Python searches through the directories
on <code>sys.path</code> looking for the package subdirectory.<P>导入模块时,Python通过 <code>sys.path</code> 中的目录列表来搜索存放包的子目录。<P>The <span class="file">__init__.py</span> files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as "<tt class="samp">string</tt>", from
unintentionally hiding valid modules that occur later on the module
search path. In the simplest case, <span class="file">__init__.py</span> can just be an
empty file, but it can also execute initialization code for the
package or set the <code>__all__</code> variable, described later.<P>必须要有一个 <span class="file">__init__.py</span> 文件的存在,才能使Python视该目录为一个包;这是为了防止某些目录使用了"<tt class="samp">string</tt>" 这样的通用名而无意中在随后的模块搜索路径中覆盖了正确的模块。最简单的情况下,<span class="file">__init__.py</span> 可以只是一个空文件,不过它也可能包含了包的初始化代码,或者设置了 <code>__all__</code> 变量,后面会有相关介绍。<P>Users of the package can import individual modules from the
package, for example:<P>包用户可以从包中导入合法的模块,例如:<P><div class="verbatim"><pre>
import Sound.Effects.echo</pre></div><P>This loads the submodule <tt class="module">Sound.Effects.echo</tt>.  It must be referenced
with its full name.<P>这样就导入了 <tt class="module">Sound.Effects.echo</tt> 子模块。它必需通过完整的名称来引用。<P><div class="verbatim"><pre>
Sound.Effects.echo.echofilter(input, output, delay=0.7, atten=4)</pre></div><P>An alternative way of importing the submodule is:<P>导入包时有一个可以选择的方式:<P><div class="verbatim"><pre>
from Sound.Effects import echo</pre></div><P>This also loads the submodule <tt class="module">echo</tt>, and makes it available without
its package prefix, so it can be used as follows:<P>这样就加载了 <tt class="module">echo</tt> 子模块,并且使得它在没有包前缀的情况下也可以使用,所以它可以如下方式调用:<P><div class="verbatim"><pre>
echo.echofilter(input, output, delay=0.7, atten=4)</pre></div><P>Yet another variation is to import the desired function or variable directly:<P>还有另一种变体用于直接导入函数或变量:<P><div class="verbatim"><pre>
from Sound.Effects.echo import echofilter</pre></div><P>Again, this loads the submodule <tt class="module">echo</tt>, but this makes its function
<tt class="function">echofilter()</tt> directly available:<P>这样就又一次加载了 <tt class="module">echo</tt> 子模块,但这样就可以直接调用它的 <tt class="function">echofilter()</tt> 函数:<P><div class="verbatim"><pre>
echofilter(input, output, delay=0.7, atten=4)

⌨️ 快捷键说明

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