[BangPypers] Clojure style multimethod functions in python
Baishampayan Ghose
b.ghose at gmail.com
Wed Aug 25 07:45:38 CEST 2010
>> 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
More information about the BangPypers
mailing list