1 min read

Python RLCompleter Module

The rlcompleter module defines a completion function suitable for the readline module by completing valid Python identifiers and keywords.

Importing the Module

import rlcompleter
import readline

Basic Usage

To use rlcompleter, you typically just need to import it and configure readline to use its completer function.

import rlcompleter
import readline

readline.parse_and_bind("tab: complete")

This enables tab completion for Python identifiers in the interactive interpreter.

The Completer Class

The module provides a Completer class.

completer = rlcompleter.Completer()

You can pass a dictionary (namespace) to the constructor to restrict completion to that namespace.

my_locals = {'hello': 'world', 'foo': 'bar'}
completer = rlcompleter.Completer(my_locals)

print(completer.complete('he', 0)) # Output: 'hello'

programming/python/python