PythOnRails@programming.dev to Python@programming.dev · 1 year agoThe Python dictionary dispatch patternjamesg.blogexternal-linkmessage-square10fedilinkarrow-up131arrow-down10cross-posted to: hackernews@derp.foo
arrow-up131arrow-down1external-linkThe Python dictionary dispatch patternjamesg.blogPythOnRails@programming.dev to Python@programming.dev · 1 year agomessage-square10fedilinkcross-posted to: hackernews@derp.foo
minus-squarevoxellinkfedilinkarrow-up6arrow-down2·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);