Ovld – Efficient and featureful multiple dispatch for Python

https://news.ycombinator.com/rss Hits: 18
Summary

Ovld Fast multiple dispatch in Python, with many extra features. 📋 Documentation With ovld, you can write a version of the same function for every type signature using annotations instead of writing an awkward sequence of isinstance statements. Unlike Python's singledispatch , it works for multiple arguments. ⚡️ Fast: ovld is the fastest multiple dispatch library around, by some margin. ovld is the fastest multiple dispatch library around, by some margin. 🚀 Variants , mixins and medleys of functions and methods. , and of functions and methods. 🦄 Dependent types: Overloaded functions can depend on more than argument types: they can depend on actual values. Overloaded functions can depend on more than argument types: they can depend on actual values. 🔑 Extensive: Dispatch on functions, methods, positional arguments and even keyword arguments (with some restrictions). Dispatch on functions, methods, positional arguments and even keyword arguments (with some restrictions). ⚙️ Codegen: (Experimental) For advanced use cases, you can generate custom code for overloads. Install with pip install ovld Example Define one version of your function for each type signature you want to support. ovld supports all basic types, plus literals and value-dependent types such as Regexp . from ovld import ovld from ovld . dependent import Regexp from typing import Literal @ ovld def f ( x : str ): return f"The string { x !r } " @ ovld def f ( x : int ): return f"The number { x } " @ ovld def f ( x : int , y : int ): return "Two numbers!" @ ovld def f ( x : Literal [ 0 ]): return "zero" @ ovld def f ( x : Regexp [ r"^X" ]): return "A string that starts with X" assert f ( "hello" ) == "The string 'hello'" assert f ( 3 ) == "The number 3" assert f ( 1 , 2 ) == "Two numbers!" assert f ( 0 ) == "zero" assert f ( "XSECRET" ) == "A string that starts with X" Recursive example ovld shines particularly with recursive definitions, for example tree maps or serialization. Here we define a function tha...

First seen: 2025-06-01 04:30

Last seen: 2025-06-01 21:33