What's the best alternative function of Lua's global pattern substitution function string.gsub(s, pattern, function) in Python? e.g.
> string.gsub("banana", "(a)(n)", function(a,b) return b..a end) -- reverse any "an"s
bnanaa 2
UPDATE: I'd like to implement a complex function during pattern substitution in Python like Lua's string.gsub, which can be implemented by Python's re.sub with function or lambda expr.