On Tue, May 28, 2013 at 3:41 PM, Russell E. Owen <ro...@uw.edu> wrote:
> Is it true that this cannot be used for instance and class methods? It
> dispatches based on the first argument, which is "self" for instance
> methods, whereas the second argument would almost certainly be the
> argument one would want to use for conditional dispatch.

You can use a staticmethod and then delegate to it, of course.  But it
probably wouldn't be too difficult to allow specifying which argument
to dispatch on, e.g.:

    @singledispatch.on('someArg')
    def my_method(self, someArg, ...):
          ...

The code would look something like this:

    def singledispatch(func, argPosn=0):
        ...
        # existing code here...
        ...
        def wrapper(*args, **kw):
            return dispatch(args[argPosn].__class__)(*args, **kw)  #
instead of args[0]

    def _dispatch_on(argname):
        def decorate(func):
            argPosn = # code to find argument position of argname for func
           return dispatch(func, argPosn)
        return decorate

    singledispatch.on = _dispatch_on

So, it's just a few lines added, but of course additional doc, tests,
etc. would have to be added as well.  (It also might be a good idea
for there to be some error checking in wrapper() to raise an
approriate TypeError if len(args)<=arg.)
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to