palindrome function

Mensanator mensanator at aol.com
Sat Jul 12 15:28:06 EDT 2008


On Jul 12, 2:18�pm, Terry Reedy <tjre... at udel.edu> wrote:
> Peter Otten wrote:
> > Denis Kasak wrote:
>
> >> Basically, it reverses the list in place, so it modifies the list which
> >> called it. It does not return a /new/ list which is a reversed version
> >> of the original, as you expected it to. Since it doesn't return anything
> >> explicitly, Python makes it return None. Hence, the comparison you are
> >> doing is between the original list and a None, which is False, naturally.
> >> Try this:
>
> >> spam = ['a', 'n', 'n', 'a']
> >> eggs = spam[:]
> >> if spam.reverse() == eggs:
> >> � � print "Palindrome"
>
> > Your explanation is correct, but your example code compares None to
> > ['a', 'n', 'n', 'a'] and therefore won't print "Palindrome", either.
>
> I don't know if this was posted yet,

It hasn't. and here's why:

IDLE 2.6b1
>>> seq=['a','n','n','a']
>>> seq.reversed()

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    seq.reversed()
AttributeError: 'list' object has no attribute 'reversed'


> but 'seq.reversed() == seq' is the
> simple way to test for 'palindomeness'.

Not in 2.x.




More information about the Python-list mailing list