Chris Barker writes:
pretty slick -- but any hope of it being as fast as a C implemented method?
I would expect not in CPython, but if "fast" matters, why are you using CPython rather than PyPy or Cython? If it matters *that* much, you can afford to write your own C implementation. But I doubt that fast matters "that much" often enough to be worth maintaining yet another string method in Python. Byte-shoveling servers might want it for bytes, though.
I've always figured that Python's rich string methods provided two things:
single method call to do common things
nice fast, pure C performance
so I think a "keep these" method would help with both of these goals.
Sure, but the translate method already gives you that, and a lot more.
Note that when you're talking about working with Unicode characters, no natural language activity I can imagine (not even translating Buddhist texts, which involves a couple of Indian scripts as well as Han ideographs) uses more than a fraction of defined characters.
So really translate with defaultdict is a specialized loop that marries an algorithmic body (which could do things like look up the original script or other character properties to decide on the replacement for the generic case) with a (usually "small") table of exceptions. That seems like inspired design to me.