[Patches] string.translate behaviour

M.-A. Lemburg mal@lemburg.com
Mon, 29 May 2000 16:20:11 +0200


Peter Schneider-Kamp wrote:
> 
> "M.-A. Lemburg" wrote:
> >
> > Note that Unicode uses a new approach here (which I find much
> > more useful, BTW):
> >
> > """
> > S.translate(table) -> unicode
> >
> > Return a copy of the string S, where all characters have been mapped
> > through the given translation table, which must be a mapping of
> > Unicode ordinals to Unicode ordinals or None. Unmapped characters
> > are left untouched. Characters mapped to None are deleted.
> > """
> 
> Okay, maybe I am missing the point, but if I want to change
> a to b, b to c and c to a I would like to write:
> 
> s.translate("abc","bca")
> 
> But as far as I can see I have to write something like this:
> 
> s.translate(range(97)+[98,99,97])
> 
> to get this behaviour. Not exactly intuitive.

The fun part about this is that you can use any mapping to
define the translation... using sequences as you did is
just one way, a dictionary would probably be a better idea
-- any object with __getitem__ will do.

Also see the generic character mapping codec which is the basis for
all code page codecs in the Unicode implementation.

Even though I agree that mapping *ordinals* is not really
intuitive, it is certainly fast and efficient (since ordinals
can also refer to sequence items).

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/