palindrome iteration

Arnaud Delobelle arnodel at googlemail.com
Sat Aug 28 05:57:52 EDT 2010


Paul Rubin <no.email at nospam.invalid> writes:

> Ian <hobson42 at gmaiil.com> writes:
>>  On 27/08/2010 21:51, Jussi Piitulainen wrote:
>>> Meanwhile, I have decided to prefer this:
>>>
>>> def palindromep(s):
>>>      def reversed(s):
>>>          return s[::-1]
>>>      return s == reversed(s)
>> I like this.
>> s[::-1] is obscure and non-obvious, especially to Python noobs.

It may be non-obvious to newcomers, but it is quite a well known idiom.
Also, I an not aware that it is customary in python to name predicate
functions with a "p" suffix - Python is not Lisp!

>
> Overriding the 'reversed' builtin even in an inner scope is a little bit
> ugly.

I agree.

> If you don't mind some overhead, list(s)==list(reversed(s)) (using the
> built-in reversed, not the special obscure one) is pretty clear.

May I suggest a comment instead:

def ispalindrome(s):
    # s[::-1] evaluates to the string s reversed
    return s == s[::-1]

-- 
Arnaud



More information about the Python-list mailing list