[BangPypers] Clojure style multimethod functions in python

Vishal vsapre80 at gmail.com
Wed Aug 25 20:01:20 CEST 2010


Does anyone know of some speed performance comparison between

multi-methods* vs* if-elif-else* vs* class based polymorphism ?

Thanks and best regards,
Vishal

On Wed, Aug 25, 2010 at 11:15 AM, Baishampayan Ghose <b.ghose at gmail.com>wrote:

> >> Without making any change whatsoever to multi() and multi_method(),
> > the result of the following code :
> >
> > # Declare the existence of a multi method switcher
> > encounter = multi(lambda x,y : (x["Species"], y["Species"]))
> >
> > @multi_method(encounter, ("Bunny","Lion"))
> > def encounter(a1, a2):
> >    return "run-away"
> >
> > @multi_method(encounter, ("Lion","Bunny"))
> > def encounter(a1, a2):
> >    return "eat"
> >
> > @multi_method(encounter, ("Bunny","Bunny"))
> > def encounter(a1, a2):
> >    return "mate"
> >
> > @multi_method(encounter, ("Lion","Lion"))
> > def encounter(a1, a2):
> >    return "fight"
> >
> > b1 = {"Species" : "Bunny", "Other" : "Stuff"}
> > b2 = {"Species" : "Bunny", "Other" : "Stuff"}
> > l1 = {"Species" : "Lion", "Other" : "Stuff"}
> > l2 = {"Species" : "Lion", "Other" : "Stuff"}
> >
> > print encounter(b1, b2)
> > print encounter(b1, l1)
> > print encounter(l1, b1)
> > print encounter(l1, l2)
> >
> > *is
> > *
> > mate
> > run-away
> > eat
> > fight
> >
> > Is that consistent with the expectations ?
>
> Fair enough. Good work, Dhananjay.
>
> Regards,
> BG
>
> --
> Baishampayan Ghose
> b.ghose at gmail.com
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list