Package: methoddispatch
Python 3.4 added thesingledispatchdecorator to thefunctoolsstandard library module.
Python 3.8 added thesingledispatchmethoddecorator to thefunctoolsstandard library module,
however it does not allow sub-classes to modify the dispatch table independantly of the base class.This library adds this functionality.To define a generic method , decorate it with the@singledispatchdecorator. Note that the dispatch happens on the type of the first argument, create your function accordingly.
To add overloaded implementations to the function, use theregister()attribute of the generic function.
It is a decorator, taking a type parameter and decorating a function implementing the operation for that type.
Theregister()attribute returns the undecorated function which enables decorator stacking, pickling, as well as creating unit tests for each variant independently>>> from methoddispatch import singledispatch, SingleDispatch
>>> from decimal import Decimal
>>> class MyClass(SingleDispatch):
...     @singledispatch
...     def fun(self, arg, verbose=False):
...         if verbose:
...             print("Let me just say,", end=" ")
...         print(arg)
...
...     @fun.register(int)
...     def fun_int(self, arg, verbose=False):
...         if verbose:
...             print("Strength in numbers, eh?", end=" ")
...         print(arg)
...
...     @fun.register(list)
...     def fun_list(self, arg, verbose=False):
...         if verbose:
...             print("Enumerate this:")
...         for i, elem in enumerate(arg):
...             print(i, elem)
...
...     @fun.register(float)
...     @fun.register(Decimal)
...     def fun_num(obj, arg, verbose=False):
...         if verbose:
...             print("Half of your number:", end=" ")
...         print(arg / 2)Theregister()method relys onSingleDispatch.__init_subclass__to create the actual dispatch table rather than adding the function directly.
This also means that (unlike functools.singledispatchmethod) two methods
with the same name cannot be registered as only the last one will be in the class dictionary.Functions not defined in the class can be registered with a generic method using theadd_overloadmethod.>>> def nothing(obj, arg, verbose=False):
...    print('Nothing.')
>>> MyClass.fun.add_overload(type(None), nothing)Usingadd_overloadwill affect all instances ofMyClassas if it were part of the class declaration.
When called, the generic function dispatches on the type of the first argument>>> a = MyClass()
>>> a.fun("Hello, world.")
Hello, world.
>>> a.fun("test.", verbose=True)
Let me just say, test.
>>> a.fun(42, verbose=True)
Strength in numbers, eh? 42
>>> a.fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
Enumerate this:
0 spam
1 spam
2 eggs
3 spam
>>> a.fun(None)
Nothing.
>>> a.fun(1.23)
0.615Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation.
The original function decorated with@singledispatchis registered for the baseobjecttype, which means it is used if no better implementation is found.To check which implementation will the generic function choose for a given type, use thedispatch()method>>> a.fun.dispatch(float)
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.dispatch(dict)    # note: default implementation
<function MyClass.fun at 0x103fe0000>To access all registered implementations, use the read-onlyregistryattribute>>> a.fun.registry.keys()
dict_keys([<class 'object'>, <class 'int'>, <class 'list'>, <class 'decimal.Decimal'>, <class 'float'>, <class 'NoneType'>])
>>> a.fun.registry[float]
<function MyClass.fun_num at 0x1035a2840>
>>> a.fun.registry[object]
<function MyClass.fun at 0x103fe0000>Extending the dispatch table.Subclasses can extend the type registry of the function on the base class with their own overrides.
TheSingleDispatchmixin class ensures that each subclass has it’s own independant copy of the dispatch registry>>> class SubClass(MyClass):
...     @MyClass.fun.register(str)
...     def fun_str(self, arg, verbose=False):
...         print('subclass')
...
>>> s = SubClass()
>>> s.fun('hello')
subclass
>>> b = MyClass()
>>> b.fun('hello')
helloOverriding the dispatch tableThere are two ways to override the dispatch function for a given type.
One way is to override a base-class method that is in the base class dispatch table.
Method overrides do not need to provide theregisterdecorator again to be used in the dispatch offun, you can
simply override the specific dispatch function you want to modify.>>> class SubClass2(MyClass):
...     def fun_int(self, arg, verbose=False):
...         print('subclass int')
...
>>> s = SubClass2()
>>> s.fun(1)
subclass intThe other way is to register a method with an existing type using theregistermethod.>>> class SubClass3(MyClass):
...    @MyClass.fun.register(int)
...    def fun_int_override(self, arg, verbose=False):
...        print('subclass3 int')
...
>>> s = SubClass3()
>>> s.fun(1)
subclass3 intNote that the decorator takes precedence over the method name, so if you do something like this:>>> class SubClass4(MyClass):
...    @MyClass.fun.register(str)
...    def fun_int(self, arg, verbose=False):
...        print('silly mistake')then SubClass4.fun_int is used for string arguments.>>> s = SubClass4()
>>> s.fun(1)
1
>>> s.fun('a string')
silly mistakeInstance overridesMethod overrides can be specified on individual instances if necessary>>> def fun_str(obj, arg, verbose=False):
...    print('instance str')
>>> b = MyClass()
>>> b.fun.register(str, fun_str)
<function fun_str at 0x000002376A3D32F0>
>>> b.fun('hello')
instance str
>>> b2 = MyClass()
>>> b2.fun('hello')
helloIntegration with type hintsFor functions annotated with types, the decorator will infer the type of the first argument automatically as shown below>>> class MyClassAnno(SingleDispatch):
...     @singledispatch
...     def fun(self, arg):
...         print('default')
...
...     @fun.register
...     def fun_int(self, arg: int):
...         print('int')
...
>>> class SubClassAnno(MyClassAnno):
...     @MyClassAnno.fun.register
...     def fun_float(self, arg: float):
...         print('float')
...
...     @MyClassAnno.fun.register
...     def fun_list(self, arg: typing.List[str]):
...         print('list')Note that methoddispatch ignores type specialization in annotations as only the class is used for dispatching.
This means thatfun_listwill be called for all list instances regardless of what is in the list.Accessing the methodfunvia a class will use the dispatch registry for that class>>> SubClass2.fun(s, 1)
subclass int
>>> MyClass.fun(s, 1)
1super()also works as expected using the dispatch table of the super class.>>> super(Mixin1, s).fun(1)
1The usual method resolution order applies to mixin or multiple inheritance. For example:>>> class BaseClassForMixin(SingleDispatch):
...    def __init__(self):
...        self.dispatched_by = ''
...
...    @singledispatch
...    def foo(self, bar):
...        print('BaseClass')
...        return 'default'
...
...    @foo.register(float)
...    def foo_float(self, bar):
...        print('BaseClass')
...        return 'float'
...
>>> class Mixin1(BaseClassForMixin):
...
...    @BaseClassForMixin.foo.register(int)
...    def foo_int(self, bar):
...        print('Mixin1')
...        return 'int'
...
...    @BaseClassForMixin.foo.register(str)
...    def foo_str(self, bar):
...        print('Mixin1')
...        return 'str2'
...
>>> class Mixin2(BaseClassForMixin):
...    @BaseClassForMixin.foo.register(str)
...    def foo_str2(self, bar):
...        print('Mixin2')
...        return 'str3'
...
>>> class SubClassWithMixins(Mixin2, Mixin1):
...    def foo_float(self, bar):
...        print('SubClassWithMixins')
...        return 'float'Note that even thoughMixin2has methodfoo_str2it will still overrideMixin1.foo_strin
the dispatch offoo()because they are both handlers for thestrtype and Mixin2 comes before
Mixin1 in the bases list.>>> s = SubClassWithMixins()
>>> s.foo('text')
Mixin2
'str3'
>>> s.foo(1)
Mixin1
'int'
>>> s.foo(3.2)
SubClassWithMixins
'float'