[Patches] string.translate behaviour

Guido van Rossum guido@python.org
Tue, 30 May 2000 09:53:30 -0500


> From: Peter Schneider-Kamp <petersc@stud.ntnu.no>
> 
> "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.

Actually, you would have to write

s.translate({ord('a'):ord('b'), ord('b'):ord('c'), ord('c'):ord('a')})

I think it would make more sense to change the API so that the keys
and values can be either ordinals or characters, so you can write

s.translate({'a':'b', 'b':'c', 'c':'a'})

The Unicode version should support this too.

(I'm not too keen on the ("abc", "bca") API, because in practice
people will soon want to be able to use generalizations like
("a-z", "n-za-m") which cause way too much trouble to parse.)

--Guido van Rossum (home page: http://www.python.org/~guido/)