16.9. rlcompleter
— GNU readline的完成函数¶
源代码: Lib/rlcompleter.py
rlcompeleter
通过补全有效的 Python 标识符和关键字为 readline
模块定义了一个补全功能套装.
当此模块在具有可用的 readline
模块的 Unix 平台被导入, 一个 Completer
实例将被自动创建并且它的 complete()
方法将设置为 readline
的补全器.
示例:
>>> 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. A user can add the following lines to his or her initialization file
(identified by the PYTHONSTARTUP
environment variable) to get
automatic Tab completion:
try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
在没有 readline
的平台, 此模块定义的 Completer
类仍然可以用于自定义行为.
16.9.1. Completer对象¶
Completer对象具有以下方法:
-
Completer.
complete
(text, state)¶ 为 text 返回 stateth 补全.
If called for text that doesn’t include a period character (
'.'
), it will complete from names currently defined in__main__
,__builtin__
and keywords (as defined by thekeyword
module).如果为加点的名称执行调用,它将尝试尽量求值直到最后一部分为止而不产生附带影响(函数不会被求值,但它可以生成对
__getattr__()
的调用),并通过dir()
函数来匹配剩余部分。 在对表达式求值期间引发的任何异常都会被捕获、静默处理并返回None
。