@[email protected] to [email protected] • 1 year agoThe Python dictionary dispatch patternjamesg.blogexternal-linkmessage-square10fedilinkarrow-up130arrow-down10cross-posted to: [email protected]
arrow-up130arrow-down1external-linkThe Python dictionary dispatch patternjamesg.blog@[email protected] to [email protected] • 1 year agomessage-square10fedilinkcross-posted to: [email protected]
minus-squarevoxellinkfedilink4•edit-21 year 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);