Build Your Own singledispatch

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 14. Everything before it counts as cleared.

functools.singledispatch picks an implementation based on the type of the first argument. Build your own (without using functools.singledispatch).

Implement a decorator dispatch(func). It returns a wrapper with:

  • register(cls): a decorator that registers an implementation for cls. It can also be used bare (@f.register), in which case the class comes from the annotation of the function's first parameter.
  • registry: a dict from class to function, including object mapped to the original function
  • calling the wrapper: find the implementation for the first class in type(arg).__mro__ that's registered, so subclasses inherit their parent's implementation and bool falls back to int
#decorators#functions#types2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Build Your Own singledispatch · TechGradify