{"library":"methoddispatch","title":"Method Dispatch","description":"methoddispatch provides a decorator similar to `functools.singledispatch` but specifically designed for dispatching class methods based on the type of their arguments. It extends Python's built-in single dispatch functionality to work seamlessly within classes, supporting different implementations for various argument types. The current version is 5.0.1, with an irregular release cadence driven by feature enhancements and Python version compatibility.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install methoddispatch"],"cli":null},"imports":["from methoddispatch import methoddispatch"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"from methoddispatch import methoddispatch\n\nclass MyClass:\n    @methoddispatch\n    def process(self, arg):\n        return f\"Processing generic {type(arg).__name__}: {arg}\"\n\n    @process.register(int)\n    def _(self, arg: int):\n        return f\"Processing int: {arg * 2}\"\n\n    @process.register(str)\n    def _(self, arg: str):\n        return f\"Processing str: {arg.upper()}\"\n\n    @process.register(list) # Will match list[int] as per v5.0.0 behavior\n    def _(self, arg: list):\n        return f\"Processing list of length: {len(arg)}\"\n\n# Example usage\ninstance = MyClass()\nprint(instance.process(10))\nprint(instance.process(\"hello\"))\nprint(instance.process([1, 2, 3]))\nprint(instance.process(3.14))\nprint(instance.process([4, 5])) # Demonstrates list[int] matching list handler","lang":"python","description":"This quickstart demonstrates how to define a class method with the `@methoddispatch` decorator and register different implementations for specific argument types. The generic `process` method serves as the fallback, while `int`, `str`, and `list` types have specialized implementations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}