# invert or reverse a string... warning this is a rant

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sat Oct 21 07:43:02 CEST 2006

```On Thu, 19 Oct 2006 22:22:34 -0300, Gabriel Genellina wrote:

>> > (1) The name is bad. "invert" is not the same as "reverse". Here's an
>> > invert: 1/2 = 0.5. Your function falsely claims numbers aren't
>> > invertable.
>>
>>Dictionary.com
>>invert = to reverse in position, order, direction, or relationship.
>
> Note that even on a mathematical sense, "inverse" does not mean only
> "multiplicative inverse". -3 is the "additive inverse" of 3, by example

A specialist meaning to invert that even mathematicians don't use unless
they are being explicitly formal.

> (BTW,
> integers -except two of them- are not inversible in Z).

Is that multiplicative inverse, or additive inverse, or both? (That's a
rhetorical question -- you're obviously talking about multiplicative
inverse, in which case the two integers with inverses are +1 and -1.)

In any case, that would be relevant if the function was for arithmetic in
Z. But it isn't -- it is for reversing sequences. Which supports my
contention that invert is a bad name.

> So "invert" is not a bad name "per se".

In ordinary English, "invert" is rarely used for left to right reversal.
(I say "rarely" because there is always somebody who will hammer the
square peg of a word with one meaning into the round hole of a related but
different meaning.) Sometimes (particular in chemistry) is used for
mirror-image reversal, but reversal of a sequence isn't the same as
reflection. (Think of the shapes of the individual character glyphs, or if
you prefer, the bit patterns of the bytes.)

> But having the reversed() builtin and reverse()
> list method implies that a similarly rooted name would be better (just
> to be coherent!) - perhaps reversed_string()?

But it does/shouldn't apply only to strings. What's wrong with just
reverse()?

--
Steven.

```