PythOnRails@programming.dev to Python@programming.dev · 1 year agoThe Python dictionary dispatch patternjamesg.blogexternal-linkmessage-square10fedilinkarrow-up130arrow-down10
arrow-up130arrow-down1external-linkThe Python dictionary dispatch patternjamesg.blogPythOnRails@programming.dev to Python@programming.dev · 1 year agomessage-square10fedilink
minus-squarevoxel@sopuli.xyzlinkfedilinkarrow-up5arrow-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);