rlcompleterCompletion function for GNU readlineGNU readline的完成功能

Source code: Lib/rlcompleter.py


The rlcompleter module defines a completion function suitable for the readline module by completing valid Python identifiers and keywords.rlcompleter模块通过完成有效的Python标识符和关键字来定义适合readline模块的完成函数。

When this module is imported on a Unix platform with the readline module available, an instance of the Completer class is automatically created and its complete() method is set as the readline completer.在Unix平台上导入此模块时,如果readline模块可用,则会自动创建Completer类的实例,并将其complete()方法设置为readline补全器。

Example:

>>> import rlcompleter
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> readline. <TAB PRESSED>
readline.__doc__ readline.get_line_buffer( readline.read_init_file(
readline.__file__ readline.insert_text( readline.set_completer(
readline.__name__ readline.parse_and_bind(
>>> readline.

The rlcompleter module is designed for use with Python’s interactive mode. rlcompleter模块设计用于Python的交互模式。Unless Python is run with the -S option, the module is automatically imported and configured (see Readline configuration).除非Python使用-S选项运行,否则会自动导入和配置模块(请参阅Readline配置)。

On platforms without readline, the Completer class defined by this module can still be used for custom purposes.在没有readline的平台上,此模块定义的Completer类仍然可以用于自定义目的。

Completer Objects完成符对象

Completer objects have the following method:完成符对象具有以下方法:

Completer.complete(text, state)

Return the stateth completion for text.返回text的第state次补全。

If called for text that doesn’t include a period character ('.'), it will complete from names currently defined in __main__, builtins and keywords (as defined by the keyword module).如果为不包含句点字符('.')的text调用,它将从当前在__main__中定义的名称、builtins和关键字(由关键字模块定义)完成。

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 __getattr__()) up to the last part, and find matches for the rest via the dir() function. 如果调用虚线名称,它将尝试评估没有明显副作用的任何内容(不会评估函数,但它可以生成对__getattr__()的调用),直到最后一部分,并通过dir()函数查找其余部分的匹配项。Any exception raised during the evaluation of the expression is caught, silenced and None is returned.在表达式求值期间引发的任何异常都将被捕获、静默,并且会返回None