summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Frlcompleter.html
blob: 0bc005cbcf5f51a500d2e54eeea598ce063ff8ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 <span id="rlcompleter-completion-function-for-gnu-readline"></span><h1>rlcompleter — Completion function for GNU readline</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/rlcompleter.py">Lib/rlcompleter.py</a></p>  <p>The <code>rlcompleter</code> module defines a completion function suitable to be passed to <a class="reference internal" href="readline#readline.set_completer" title="readline.set_completer"><code>set_completer()</code></a> in the <a class="reference internal" href="readline#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a> module.</p> <p>When this module is imported on a Unix platform with the <a class="reference internal" href="readline#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a> module available, an instance of the <a class="reference internal" href="#rlcompleter.Completer" title="rlcompleter.Completer"><code>Completer</code></a> class is automatically created and its <a class="reference internal" href="#rlcompleter.Completer.complete" title="rlcompleter.Completer.complete"><code>complete()</code></a> method is set as the <a class="reference internal" href="readline#readline-completion"><span class="std std-ref">readline completer</span></a>. The method provides completion of valid Python <a class="reference internal" href="../reference/lexical_analysis#identifiers"><span class="std std-ref">identifiers and keywords</span></a>.</p> <p>Example:</p> <pre data-language="python">&gt;&gt;&gt; import rlcompleter
&gt;&gt;&gt; import readline
&gt;&gt;&gt; readline.parse_and_bind("tab: complete")
&gt;&gt;&gt; readline. &lt;TAB PRESSED&gt;
readline.__doc__          readline.get_line_buffer(  readline.read_init_file(
readline.__file__         readline.insert_text(      readline.set_completer(
readline.__name__         readline.parse_and_bind(
&gt;&gt;&gt; readline.
</pre> <p>The <code>rlcompleter</code> module is designed for use with Python’s <a class="reference internal" href="../tutorial/interpreter#tut-interactive"><span class="std std-ref">interactive mode</span></a>. Unless Python is run with the <a class="reference internal" href="../using/cmdline#cmdoption-S"><code>-S</code></a> option, the module is automatically imported and configured (see <a class="reference internal" href="site#rlcompleter-config"><span class="std std-ref">Readline configuration</span></a>).</p> <p>On platforms without <a class="reference internal" href="readline#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a>, the <a class="reference internal" href="#rlcompleter.Completer" title="rlcompleter.Completer"><code>Completer</code></a> class defined by this module can still be used for custom purposes.</p> <span class="target" id="completer-objects"></span><dl class="py class"> <dt class="sig sig-object py" id="rlcompleter.Completer">
<code>class rlcompleter.Completer</code> </dt> <dd>
<p>Completer objects have the following method:</p> <dl class="py method"> <dt class="sig sig-object py" id="rlcompleter.Completer.complete">
<code>complete(text, state)</code> </dt> <dd>
<p>Return the next possible completion for <em>text</em>.</p> <p>When called by the <a class="reference internal" href="readline#module-readline" title="readline: GNU readline support for Python. (Unix)"><code>readline</code></a> module, this method is called successively with <code>state == 0, 1, 2, ...</code> until the method returns <code>None</code>.</p> <p>If called for <em>text</em> that doesn’t include a period character (<code>'.'</code>), it will complete from names currently defined in <a class="reference internal" href="__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a>, <a class="reference internal" href="builtins#module-builtins" title="builtins: The module that provides the built-in namespace."><code>builtins</code></a> and keywords (as defined by the <a class="reference internal" href="keyword#module-keyword" title="keyword: Test whether a string is a keyword in Python."><code>keyword</code></a> module).</p> <p>If called for a dotted name, it will try to evaluate anything without obvious side-effects (functions will not be evaluated, but it can generate calls to <a class="reference internal" href="../reference/datamodel#object.__getattr__" title="object.__getattr__"><code>__getattr__()</code></a>) up to the last part, and find matches for the rest via the <a class="reference internal" href="functions#dir" title="dir"><code>dir()</code></a> function. Any exception raised during the evaluation of the expression is caught, silenced and <a class="reference internal" href="constants#None" title="None"><code>None</code></a> is returned.</p> </dd>
</dl> </dd>
</dl> <div class="_attribution">
  <p class="_attribution-p">
    &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
    <a href="https://docs.python.org/3.12/library/rlcompleter.html" class="_attribution-link">https://docs.python.org/3.12/library/rlcompleter.html</a>
  </p>
</div>