palindrome iteration

Dave Angel davea at ieee.org
Sat Aug 28 16:53:12 EDT 2010


Jussi Piitulainen wrote:
> Steven D'Aprano writes:
>   
>> On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote:
>>     
>>> Terry Reedy writes:
>>>       
>>>> On 8/27/2010 3:43 PM, Jussi Piitulainen wrote:
>>>>         
>>>>> Dave Angel writes:
>>>>>           
>>>>> There could easily be a .reverse() method on strings. It would return
>>>>> the reversed string, like .swapcase() returns the swapcased string.
>>>>>           
>>>> Could be, but the main use case seems to be for palindrome testing ;-)
>>>> Given that slicing and reversed() can do the same thing, the need is
>>>> thin.
>>>>         
>>> The need is quite thin, but immutability of strings is not an issue,
>>> just like there can be .swapcase() though strings are immutable. That is
>>> all I am saying above.
>>>       
>> You're right, there could be a reversed() method for strings. There
>> could also be a disemvowel method that removes vowels, a randomise
>> method that shuffles the letters around, a studlycaps method that
>> changes the case of each letter randomly, and a method to check that
>> brackets () are well- formed. They would all be useful to
>> somebody. There are lots of different methods that strings could
>> have. Where do you draw the line?
>>     
>
> When I said that there could be such a method, I was merely objecting
> to a statement, made in response to me, that there could not be such a
> method because strings are immutable. You clearly agree with me that
> that statement was not correct. Would you have let it stand if it was
> made to you?
>
>   
Since you repeat that assertion three times, I figure you must think 
it's important.  And it was I who asserted that a reverse() method 
wouldn't be possible on an immutable object.  reverse() would reverse 
the characters in place, and return None.  At least it would if it tried 
to be at all consistent with the list, array, and audioop methods of the 
same name.

reversed() is certainly possible, and it'd make a new string with the 
reverse order of the original.


DaveA




More information about the Python-list mailing list