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

Simon Forman rogue_pedro at yahoo.com
Fri Oct 20 09:01:58 CEST 2006


rick wrote:
...
> see that the sequence is reversed. Backwards is pure, simple and easy.
> This is not so in Python and it should be.

foo[::-1] isn't "pure, simple and easy"?

It derives cleanly from the slice notation, it does exactly what you
want, and it even works with lists and tuples and any other class
written to support slice notation.

Does ruby have a built in string function to do this: foo[::-2]?  How
about this: foo[-1::-2] or this: foo[-2::-2]?

See, it's not about "blindly memorizing this stuff", it's about clever
abstractions that allow you to quickly, flexibly and powerfully program
your computer.  I'd rather learn a few well thought out abstractions
than memorize a set of methods.

The answer to your OP question, "Why can't Python have a reverse()
function/method like Ruby?" is "It can."  You can build it yourself
quickly and easily, and a lot of other functions as well.

Peace,
~Simon




More information about the Python-list mailing list