@PythOnRails to Python • 10 months agoThe Python dictionary dispatch patternjamesg.blogexternal-linkmessage-square10arrow-up133arrow-down10cross-posted to: [email protected]
arrow-up133arrow-down1external-linkThe Python dictionary dispatch patternjamesg.blog@PythOnRails to Python • 10 months agomessage-square10cross-posted to: [email protected]
minus-squarevoxellinkfedilink4•edit-210 months agonot unique to python. function pointers and hashmaps exist in basically all languages like in lua: local table = { add = function(a, b) return a + b end } table["add"](4, 5) JavaScript: { add: (a, b) => a + b } Rust (using hashmaps with string keys is extremely inefficient tho, there are much better options here but whatever) let map = HashMap::from([ ("add", |a, b| a + b), ]); //... map["add"](1, 3);
not unique to python.
function pointers and hashmaps exist in basically all languages
like in lua:
local table = { add = function(a, b) return a + b end } table["add"](4, 5)
JavaScript:
{ add: (a, b) => a + b }
Rust (using hashmaps with string keys is extremely inefficient tho, there are much better options here but whatever)
let map = HashMap::from([ ("add", |a, b| a + b), ]); //... map["add"](1, 3);