uTools-Manuals/docs/python/rlcompleter.html
2019-04-21 11:50:48 +08:00

9 lines
6.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="body" role="main"><div class="section" id="module-rlcompleter"><h1><span class="yiyi-st" id="yiyi-10">6.8. <a class="reference internal" href="#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code class="xref py py-mod docutils literal"><span class="pre">rlcompleter</span></code></a> - GNU readline的完成功能</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>源代码:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/rlcompleter.py">Lib / rlcompleter.py</a></span></p><p><span class="yiyi-st" id="yiyi-12"><a class="reference internal" href="#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code class="xref py py-mod docutils literal"><span class="pre">rlcompleter</span></code></a>模块通过完成有效的Python标识符和关键字来定义适合<a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a>模块的完成函数。</span></p><p><span class="yiyi-st" id="yiyi-13">当在Unix平台上导入<a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a>模块时,会自动创建<code class="xref py py-class docutils literal"><span class="pre">Completer</span></code>类的实例及其<code class="xref py py-meth docutils literal"><span class="pre">complete()</span></code>方法设置为<a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a> completer。</span></p><p><span class="yiyi-st" id="yiyi-14">例:</span></p><pre><code class="language-python"><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">rlcompleter</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">readline</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">readline</span><span class="o">.</span><span class="n">parse_and_bind</span><span class="p">(</span><span class="s2">"tab: complete"</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">readline</span><span class="o">.</span> <span class="o">&lt;</span><span class="n">TAB</span> <span class="n">PRESSED</span><span class="o">&gt;</span>
<span class="go">readline.__doc__ readline.get_line_buffer( readline.read_init_file(</span>
<span class="go">readline.__file__ readline.insert_text( readline.set_completer(</span>
<span class="go">readline.__name__ readline.parse_and_bind(</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">readline</span><span class="o">.</span>
</code></pre><p><span class="yiyi-st" id="yiyi-15"><a class="reference internal" href="#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code class="xref py py-mod docutils literal"><span class="pre">rlcompleter</span></code></a>模块设计用于Python的<a class="reference internal" href="../tutorial/interpreter.html#tut-interactive"><span>interactive mode</span></a></span><span class="yiyi-st" id="yiyi-16">除非Python使用<a class="reference internal" href="../using/cmdline.html#cmdoption-S"><code class="xref std std-option docutils literal"><span class="pre">-S</span></code></a>选项运行,模块将自动导入和配置(参见<a class="reference internal" href="site.html#rlcompleter-config"><span>Readline configuration</span></a>)。</span></p><p><span class="yiyi-st" id="yiyi-17">在没有<a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a>的平台上,此模块定义的<code class="xref py py-class docutils literal"><span class="pre">Completer</span></code>类仍可用于自定义目的。</span></p><div class="section" id="completer-objects"><h2><span class="yiyi-st" id="yiyi-18">6.8.1.</span><span class="yiyi-st" id="yiyi-19">Completer对象</span></h2><p><span class="yiyi-st" id="yiyi-20">Completer对象有以下方法</span></p><dl class="method"><dt id="rlcompleter.Completer.complete"><span class="yiyi-st" id="yiyi-21"><code class="descclassname">Completer.</code><code class="descname">complete</code><span class="sig-paren">(</span><em>text</em>, <em>state</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-22"><em>文本</em>返回<em>状态</em>完成。</span></p><p><span class="yiyi-st" id="yiyi-23">If called for <em>text</em> that doesnt include a period character (<code class="docutils literal"><span class="pre">'.'</span></code>), it will complete from names currently defined in <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal"><span class="pre">__main__</span></code></a>, <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal"><span class="pre">builtins</span></code></a> and keywords (as defined by the <a class="reference internal" href="keyword.html#module-keyword" title="keyword: Test whether a string is a keyword in Python."><code class="xref py py-mod docutils literal"><span class="pre">keyword</span></code></a> module).</span></p><p><span class="yiyi-st" id="yiyi-24">如果调用点名称,它将尝试评估任何没有明显副作用(函数不会被评估,但它可以生成调用<a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></code></a>)直到最后一部分,并找到通过<a class="reference internal" href="functions.html#dir" title="dir"><code class="xref py py-func docutils literal"><span class="pre">dir()</span></code></a>函数匹配其余部分。</span><span class="yiyi-st" id="yiyi-25">在表达式求值期间引发的任何异常都被捕获,静默并返回<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a></span></p></dd></dl></div></div></div>