rlcompleter — Completion function for GNU readline

Source code:Lib/rlcompleter.py


Therlcompleter module defines a completion function suitable for thereadline module by completing valid Python identifiers and keywords.

When this module is imported on a Unix platform with thereadline moduleavailable, an instance of theCompleter class is automatically createdand itscomplete() method is set as thereadline completer.

Example:

>>>importrlcompleter>>>importreadline>>>readline.parse_and_bind("tab: complete")>>>readline.<TABPRESSED>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.

Therlcompleter module is designed for use with Python’sinteractive mode. Unless Python is run with the-S option, the module is automatically imported and configured(seeReadline configuration).

On platforms withoutreadline, theCompleter class defined bythis module can still be used for custom purposes.

Completer Objects

Completer objects have the following method:

Completer.complete(text,state)

Return thestateth completion fortext.

If called fortext that doesn’t include a period character ('.'), it willcomplete from names currently defined in__main__,builtins andkeywords (as defined by thekeyword module).

If called for a dotted name, it will try to evaluate anything without obviousside-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 thedir() function. Any exception raised during the evaluation of theexpression is caught, silenced andNone is returned.