easy +8 pts

Exit handler registration

Implement a helper that registers cleanup callbacks to run when the program exits, returning success status.

Implement a function `register_exit_handler(callback)` that registers the given `callback` to be called when the Python interpreter exits. The function should return `True` if the registration was successful, and `False` if the callback is not callable or registration fails (e.g., because the interpreter is already shutting down). Use Python's `atexit` module. Your function must not call the callback; it only registers it. The callback will be invoked later by the runtime. Signature: `def register_exit_handler(callback) -> bool:`

Constraints

The `callback` may be any object. Only callable objects (functions, bound methods, lambdas, etc.) should be registered. The implementation must not raise exceptions; return `False` for invalid input or registration failure. The function should work even if `atexit.register` raises an exception (rare).

Example

>>> def cleanup():
...     print("Cleaning up...")
>>> result = register_exit_handler(cleanup)
>>> result
True
>>> register_exit_handler(42)
False
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

First check if `callback` is callable using `callable()`.
Use `atexit.register(callback)` inside a try/except.
Return `True` when registration succeeds, `False` otherwise.
Do not call the callback yourself.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.