Python Registry Pattern vs if-elif: When It's Worth Switching
By Matt Cullerton, founder of Mavric and Apso
The registry pattern replaces a long if/elif chain in Python with a dictionary that maps keys to functions, classes or instances, usually filled in by a decorator. A TikTok from @realpython.com, cut from the Real Python Podcast, says to stop using if-else chains and use it instead. The source article it draws on is more careful: it says a registry is overkill for a few stable branches and wins only when you dispatch on a discrete key to a set of behaviors you expect to grow.
What is the registry pattern in Python?
The KDnuggets article the clip discusses, "Stop Using If-Else Chains: Use the Registry Pattern in Python Instead" by Kanwal Mehreen, defines it as "a central lookup table that maps keys to objects (functions, classes, instances), where each object registers itself instead of being hardcoded into some conditional. In Python, that lookup table is almost always a dictionary, and 'registering' is usually done with a decorator."
Real Python's glossary entry, by Martin Breuss, describes it as "a central lookup table mapping keys to objects, so any part of a program can find a component by name." It credits the pattern to Martin Fowler's Patterns of Enterprise Application Architecture.
What's wrong with long if-elif chains?
The clip lists four problems from the KDnuggets article:
- It violates the open-closed principle. "New case, new edit to a function that already worked."
- It scales badly. "Twenty branches is twenty things to scroll past every time you are debugging branch number three."
- It cannot be extended from outside. If a library ships a hardcoded chain, users "cannot add their own model without monkey-patching or forking."
- It piles unrelated logic into one place. The clip skips this one.
The clip says chains grow to "a hundred lines." The article says 200. Neither number is measured.
The open-closed wording, "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification," is Robert C. Martin's paraphrase in The C++ Report. Martin credits the principle to Bertrand Meyer's Object Oriented Software Construction (1988).
When should you not use a registry?
The clip leaves out the article's own condition: "Do not reach for it too early. A registry is overkill for two or three stable branches whose logic genuinely differs. If the branches share no common signature, or the conditions are ranges rather than discrete keys (if score > 0.9 ... elif score > 0.5 ...), a plain conditional is clearer."
The article also warns about three pitfalls:
- "Registration only happens on import." If nothing imports the module holding a handler, its decorator never runs and the handler goes missing.
- "Guard against silent overwrites." Two components registering the same key overwrite each other.
- "Show people what is available." Put
list(registry.keys())in your error messages.
Real Python's glossary adds that a registry "attracts the same criticisms as a singleton. It holds mutable global state and hides dependencies," and that "a missing key also surfaces at runtime rather than at import time, and registration order matters." Both failures reproduced on CPython 3.10.14: an unregistered key raised KeyError at call time, and a key from a module that had not yet run was missing.
Is a dict registry faster than if-elif?
Slightly, and only for keys deep in the chain. The Python wiki lists dict lookups as O(1) on average. A test on CPython 3.10.14 with 20 branches, one million calls per setup and the best of seven runs gave:
- Match on branch 1: chain 73.7 ns, dict 72.9 ns.
- Match on branch 10: chain 168.9 ns, dict 78.9 ns.
- Match on branch 20: chain 257.3 ns, dict 80.8 ns.
The dict stays flat while the chain grows with position. The gap is measured in nanoseconds, so speed is not a strong reason to switch. Readability and outside extensibility are.
What about Python's match statement?
The clip presents only two options. Python 3.10 added a third with match, specified in PEP 634 by Brandt Bucher and Guido van Rossum. It runs "the first case block whose patterns succeeds matching it," checking cases in order. That makes it easier to read than an elif ladder, but it does not give constant-time lookup the way a dict does.
Does Python's standard library use registries?
Yes, two of them. functools.singledispatch, added in Python 3.4, dispatches on the type of the first argument. You add implementations with its register() decorator, and its registry attribute is read-only. On CPython 3.10.14 it is a mappingproxy, and assigning into it raises TypeError.
The codecs module keeps a codec registry. lookup() "looks up the codec info in the Python codec registry," checking a cache and then the registered search functions.
Where the clip comes from
The burned-in URL on screen is the KDnuggets article. The TikTok description links Real Python's glossary page instead, and Real Python Podcast episode #305, "Should You Understand Your Entire Python Codebase?" from 31 July 2026, whose show notes list the KDnuggets article at 00:06:42. The description names "Chris Trudeau (hosted by Chris Bailey)," while the podcast's own pages use Christopher Trudeau and Christopher Bailey.
Bottom line
A dict registry is a good fit when you dispatch on a discrete key to interchangeable handlers and expect to add more, especially if other code needs to register its own. For two or three stable branches or range checks, keep the conditional. Watch for import-order bugs and silent overwrites, and check functools.singledispatch before building your own.
Sources
- Stop Using If-Else Chains (KDnuggets): the article the clip recites.
- Registry pattern (Real Python glossary): definition, stdlib examples and caveats.
- Podcast episode #305: the episode the clip comes from.
- Show index page: host name.
- RSS feed: episode date.
- functools documentation:
singledispatchand its read-only registry. - codecs documentation: the codec registry.
- TimeComplexity (Python wiki): dict lookup complexity.
- PEP 634: the
matchstatement. - The Open-Closed Principle, Robert C. Martin (archived PDF): origin of the principle's wording.
Published August 21, 2026. Based on a TikTok by @realpython.com. Full video notes (markdown)