mrocklin/multipledispatch — pull request #5 by jlowin (2014-02-28 17:20:38+00:00)
Title: Support for bound methods at runtime

Previously, detecting methods depended on looking for a `self` argument which isn't robust, since users could easily call the first argument something other than `self`.

But fundamentally, the issue is this: at compile time, there is no difference between a method named `f` and an equivalent function named `f` except that one has more arguments than the other (namely, `self`). Decoration (and consequently, dispatching) takes place at compile time. Binding the function (and more importantly, making it identifiable as a bound method) takes place at run time, after the dispatching has finished. So how can the dispatcher know if the function is bound and needs to be passed `self`?

One way around this is to use the `__get__()` descriptor method, which is only called if `Dispatcher` is being accessed as a attribute of a class. We make the assumption that if __get__ is called, the user is trying to call a bound method. This PR watches for that situation and adds an instance attribute to the dispatcher. Then when the dispatcher's `__call__` method runs, if it finds the instance attribute, it includes it in the function call (taking the place of 'self') and then resets it so it won't add it every time.

This still has drawbacks... since functions are dispatched by name, methods can be overwritten by similarly-named functions that are defined later in the program, which causes confusion because Dispatcher will add `self` and then call the most recent function (that isn't a method). Appreciate any thoughts on how to handle this...