On this page
rlcompleter
Prev: resource | Up: stdlib_module | Next: runpy
Rel: string, re, difflib, textwrap, unicodedata
Ref: docs.python.org — rlcompleter
Tags: #public #python3 #stdlib
Import
import rlcompleter
Overview
Tab-completion provider (Completer) for the interactive Python REPL, hooking into readline to complete global names, keywords, and object attributes (obj.<Tab>). Use rlcompleter when embedding a custom Python console with code so users get standard Python tab completion.
Quick Start
import rlcompleter
comp = rlcompleter.Completer({"alpha": 1, "alphabet": 2, "beta": 3})
assert comp.complete("alp", 0) == "alpha"
assert comp.complete("alp", 1) == "alphabet"
assert comp.complete("alp", 2) is None
Implementation Walkthrough
1. Wiring rlcompleter Into a Custom REPL
Pass your console's locals dict to Completer(namespace) and bind it via readline.set_completer(comp.complete):
import rlcompleter
ns = {"items": [1, 2, 3]}
comp = rlcompleter.Completer(ns)
# Attribute completion on objects in ns:
assert comp.complete("items.app", 0) == "items.append("
Gotchas & Boundaries
- Attribute completion evaluates the prefix: Completing
expr.attrevaluatesexprin the namespace viaeval(); avoid exposingrlcompleteron untrusted input or objects with expensive@propertyside effects. - Requires readline for live terminal binding:
Completeritself is pure Python and works anywhere, but interactive tab-binding depends onreadline(Unix).
Classes & Objects
- rlcompleter.Completer —
rlcompleter.Completerclass. (Methods:.attr_matches(),.complete(),.global_matches())
Linked from
Related notes
- tuts/python3/stdlib_module/resource Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/rlcompleter/Completer Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/runpy Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/code Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/difflib Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/re Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/readline Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/string Linked here; shared tags: #stdlib