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