Possibility and Probability

A Python programmer with a personality thinking about space exploration

21 July 2010

Python function overloading via multimethods

by nickadmin

One of my favorite features of OO languages like Java is the ability to overload a method, which is where you have several methods with the same name, but they take in different types in their arguments. I love the python language, but as I blogged about a while ago, not having overloaded methods in python is kind of a bummer. But do you have to trade your dynamic freedom in for static types to gain this? It turns out that with a little ingenuity, you can get that functionality using the generic dispatch pattern and python’s decorator syntax. The example on the above wiki page is nice and concise, but I think that Alex Gaynor’s blog post about multimethods is the best example of how to implement this useful idea. In leiu of an explanation of how it works, Alex gives us a unit test that exercises the mutimethod class and the dispatch/overloading idea that it brings to life. As long as you know what a decorator is the code does make sense. There are other examples of using a dispatch, but I feel that Alex’s is more clear. These are of course no substitute for having function overloading built into the python language, but having the functionality wrapped up in a nice neat class like this is a very good thing.

tags: