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

Rainy ak at silmarill.org
Thu Oct 19 23:41:40 EDT 2006


Brad wrote:
> John Salerno wrote:
> > rick wrote:
> >> Why can't Python have a reverse() function/method like Ruby?
> >
> > I'm not steeped enough in daily programming to argue that it isn't
> > necessary, but my question is why do you need to reverse strings? Is it
> > something that happens often enough to warrant a method for it?
>
> I'm home for lunch so my email addy is different.
>
> No, it doesn't happen very often, but when I need to reverse something
> (usually a list or a string). I can never remember right of the top of
> my head how to do so in Python. I always have to Google for an answer or
> refer back to old code.
>
> IMO, I should be able to intuitively know how to do this. Python is so
> user-friendly most every place else... why can it not be so here?
>
> I wrote this so I'll never have to remember this again:
>
> def invert(invertable_object):
>      try:
>          print invertable_object[::-1]
>          return invertable_object[::-1]
>      except:
>          print 'Object not invertable'
>          return 1
>
> invert([1,2,3,4])
> invert('string')
> invert({1:2, 3:4})

A sequence can be reversed with a .reverse() method. For my needs, in 7
years of programming, I never needed to reverse a string. I think it
sounds like a reasonable rule of thumb to set aside things that you do
fairly often (let's say at least once a week or once in two weeks as
you write code, on average), and make sure you have shortcuts for all
of them, ways to write them quickly and easily, and make it easy to
remember as well. All other things, things you do less often, need not
be done so quickly and easily. Otherwise, I think you'll end up with a
language that has too many built in functions, methods, ins, outs and
what have you's.

If I had to reverse a string I'd first try the reverse method (although
my immediate feeling would be that it's probably not there), and then
I'd use a few lines to make a list, reverse it and join it. I don't
usually use one-liners. I would reason to myself that once in a few
years it's not a hardship to use a couple extra lines of code.

But maybe I'm missing something and in some problem domain there is a
frequent need to reverse a string? I saw your comment about countdown
on a launch of a shuttle, but that's not convincing to me, to be
honest, because if you wished to do anything as you count down (i.e.
print the next second number), you'd just make a loop and you can make
range() command reverse, or you can just reverse the list it returns.

It's a little strange; you do get a feeling that reversing a string
might be useful sometime or other, but the only thing I can think of is
palindromes, as someone else mentioned, which is used in some
textbooks. However, isn't it a little silly to add a function or a
method to language to make each textbook example into a one-liner?




More information about the Python-list mailing list