[Python-ideas] More user-friendly version for string.translate()
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Tue Nov 1 03:15:53 EDT 2016
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:
> 1) single method call to do common things
> 2) nice fast, pure C performance
> so I think a "keep these" method would help with both of these
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.
More information about the Python-ideas