Function Registry for a Mini Plugin System

Hard ⏱ 18 min 25% acceptance ★★★★☆ 4.3
Build a tiny plugin registry using three functions that share a module-level dict PLUGINS = {}: register(name, func) stores func under name in PLUGINS and returns None; run_plugin(name, *args, **kwargs) looks up name in PLUGINS and calls it with the given args/kwargs, returning its result (or the string "No such plugin" if name is not registered); list_plugins() returns a sorted list of all registered plugin names.

Examples

Example 1
Input
def hello(who): return f"Hello, {who}"
register("hello", hello)
run_plugin("hello", "World")
Output
Hello, World
Explanation

run_plugin looks up "hello" in PLUGINS and calls it with "World".

Example 2
Input
list_plugins()
Output
['hello']
Explanation

Only "hello" has been registered so far, sorted alphabetically (trivially, with one element).

Example 3
Input
run_plugin("missing")
Output
No such plugin
Explanation

"missing" was never registered.

Constraints

  • PLUGINS must be a single module-level dict shared by all three functions.
  • run_plugin must forward any positional and keyword arguments to the stored function.

Topics

Functionsfirst-class functions

Companies

AtlassianServiceNowSalesforce

Hints

Hint 1

register mutates the shared dict, so it does not need the global keyword (it is not reassigning PLUGINS itself).

Hint 2

run_plugin should use PLUGINS.get(name) and check for None before calling, then call func(*args, **kwargs) to forward all arguments.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.