[BangPypers] Clojure style multimethod functions in python

Dhananjay Nene dhananjay.nene at gmail.com
Mon Aug 23 16:50:42 CEST 2010


On Mon, Aug 23, 2010 at 8:13 PM, Baishampayan Ghose <b.ghose at gmail.com>wrote:

> > While python's multimethod module allows function overloading based on
> > types, here's another approach to do the same based on a switching
> function
> > (clojure style) or on a set of conditions.
> >
> > Clojure style multimethod functions in
> > python<
> http://codeblog.dhananjaynene.com/2010/08/clojure-style-multi-methods-in-python/
> >
>
> Please don't claim that the above is `Clojure style multimethods'.
> Clojure multimethods can dispatch on an arbitrary function of `the
> arguments' given to the multimethod and not just on any arbitrary
> function of `the properties of the object'.
>

BG,

Could you help explain in light of the following (the argument lists are in
bold). The switcher function is provided exactly the same arguments as the
various multimethods. Probably something about clojure I am not aware of ?

Dhananjay

def multi(switcher_func):
    """ Declares a multi map based method which will switch to the
    appropriate function based on the results of the switcher func"""

    def dispatcher(*args, **kwargs):
        key = *switcher_func(*args, **kwargs)*
        func = dispatcher.dispatch_map[key]
        if func :
            return *func(*args,**kwargs)*
        else :
            raise Exception("No function defined for dispatch key: %s" % key
)
    dispatcher.dispatch_map = {}
    return dispatcher

>
> Regards,
> BG
>
> --
> Baishampayan Ghose
> b.ghose at gmail.com
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
--------------------------------------------------------
blog: http://blog.dhananjaynene.com
twitter: http://twitter.com/dnene


More information about the BangPypers mailing list