Right! I’ve been operating under the assumption that we would like to make existing implementations typing compliant, but you seem to want to discuss runtime details in more detail. For this, I think its helpful to study existing implementations to see works and what people like. The following would probably be good to look at:
- github .com/mrocklin/multipledispatch
- github .com/coady/multimethod
- github .com/beartype/plum
Please remove the spaces in the links above. I’m not able to post multiple links, since I’m a new user. Disclaimer: I’m one of the authors of Plum.
That’s definitely true. The first PyPI release of multipledispatch was in 2014, the first release of multimethod was in 2013, and the first release of plum-dispatch was in 2019, so I think it’s fair to say that sufficient time has passed and effort has been spent for these implementations to have converged onto something that works well, at least in practice. Unless these implementations have any obvious shortcomings, I’m not sure that there is a need to develop another.
You need not restrict yourself to either local/global namespaces or f.dispatch. You can combine them. This, in fact, is what I tend find most convenient in practice. Within a package, use a local namespace created with dispatch = Dispatcher(). When extending functions defined by other packages, use from otherpackage import f in combination with @f.dispatch. But one is always free to use whichever pattern they find most clear.
The practical problem at hand is not that one cannot effectively use multiple dispatch at runtime, whether that be with @f.dispatch or with local/global spaces, since both are possible with the implementations listed above. The problem is that it’s hard to write such code which is typing compliant too.