ATdispatcher is a flexible Python dispatcher library for creating functions and methods with multiple options (overloads), default arguments, type checking, and automatic handling of instance attributes for methods. It allows a simple API to manage multiple variations of the same function or method.
- Dispatch multiple function variations under the same name.
- Support for default arguments.
- Support for type hints to select the correct variant.
- Dispatch methods with automatic
selfandSelfAttrhandling. - Simple API with
@dispatcherfor functions and@method_dispatcherfor methods. - Easily extendable with multiple registrations using
.reg().
Currently, ATdispatcher is a standalone module. You can include it in your project directly:
git clone https://github.com/avitwil/ATdispatcher.git
or
pip install ATdispatcherOr copy ATdispatcher.py into your project directory.
from ATdispatcher import dispatcher, method_dispatcher, SelfAttr@dispatcher
def func(a: int, b: int):
return a + b
@func.reg()
def _(a: int, b: int, c: int):
return a + b + c
@func.reg()
def _(a: int, b: int, c: int = 3):
return a * b * c
print(func(5, 6)) # Output: 11 (matches first variant)
print(func(5, 6, 7)) # Output: 18 (matches second variant)
print(func(5, 6, 3)) # Output: 90 (matches third variant with default)✅ Notes:
- You can register multiple variants with different signatures using
.reg(). - Type hints are used to select the correct variant.
- Default arguments are supported.
SelfAttr allows method defaults to refer to instance attributes automatically.
class MyClass:
def __init__(self):
self.mult = 2
@method_dispatcher
def method(self, x: int, y: int = SelfAttr("mult")):
return x * y
obj = MyClass()
print(obj.method(10)) # Output: 20 (y defaults to obj.mult)
print(obj.method(10, 5)) # Output: 50 (y explicitly passed)✅ Notes:
SelfAttr("attr_name")automatically fetchesself.attr_nameas the default.- Works with multiple method registrations using
.reg()as well.
class MyClass:
def __init__(self):
self.mult = 3
@method_dispatcher
def calc(self, x: int):
return x * 2
@calc.reg()
def _(self, x: int, y: int = SelfAttr("mult")):
return x * y
obj = MyClass()
print(obj.calc(5)) # Output: 10 (first variant)
print(obj.calc(5, 4)) # Output: 20 (second variant with y=4)
print(obj.calc(5, 3)) # Output: 15 (second variant, y defaults to obj.mult)If no matching signature is found:
try:
func("a", 5)
except TypeError as e:
print(e) # Output: No matching function signature found✅ Type checking ensures invalid calls raise TypeError.
| Class / Function | Description |
|---|---|
dispatcher(func) |
Create a function dispatcher. |
.reg() |
Register a new variant for the same dispatcher. |
method_dispatcher(func) |
Create a method dispatcher for instance methods. |
SelfAttr("attr") |
Used for method default arguments that reference self attributes. |
from ATdispatcher import dispatcher, method_dispatcher, SelfAttr
@dispatcher
def add(a: int, b: int):
return a + b
@add.reg()
def _(a: int, b: int, c: int = 10):
return a + b + c
class Calc:
def __init__(self):
self.multiplier = 5
@method_dispatcher
def multiply(self, x: int, y: int = SelfAttr("multiplier")):
return x * y
calc = Calc()
print(add(1, 2)) # 3
print(add(1, 2, 3)) # 6
print(calc.multiply(4)) # 20
print(calc.multiply(4, 2))# 8MIT License – free to use, modify, and distribute.