rlcompleter — Completion function for GNU readline¶
Source code:Lib/rlcompleter.py
Therlcompleter module defines a completion function suitable to bepassed toset_completer() in thereadline module.
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. The method providescompletion of valid Pythonidentifiers and keywords.
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.
- classrlcompleter.Completer¶
Completer objects have the following method:
- complete(text,state)¶
Return the next possible completion fortext.
When called by the
readlinemodule, this method is calledsuccessively withstate==0,1,2,...until the method returnsNone.If called fortext that doesn’t include a period character (
'.'), it willcomplete from names currently defined in__main__,builtinsandkeywords (as defined by thekeywordmodule).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 therest via thedir()function. Any exception raised during theevaluation of the expression is caught, silenced andNoneisreturned.