a=[1,2,3,4].reverse() - why "a" is None?

Simon Forman sajmikins at gmail.com
Tue Oct 13 09:55:31 EDT 2009


On Mon, Oct 12, 2009 at 4:44 AM, Nadav Chernin <Nadav.C at qualisystems.com> wrote:
>
>        Chris Withers wrote:
>
>        ...becauase you were looking for:
>
>        reversed([1,2,3,4])
>
> OK, but my question is generic. Why when I use object's function that
> changed values of the object, I can't to get value of it on the fly
> without writing additional code?
>
>>>> a=[1,3,2,4]
>>>> a.sort()
>>>> a.reverse()
>>>> a
> [4, 3, 2, 1]
>
> Why I can't a==[1,3,2,4].sort().reverse() ?

That's just the way it is with list objects.

Note that string objects do work that way:

In [1]: "FOO".lower().replace('o', 'a')
Out[1]: 'faa'

But this is because string objects are immutable in python, so their
methods must return new strings.

Bottom line: read the documentation.  (In the python interpreter you
can use the built-in function help() on any object or function or
method to get information on it.)

HTH,
~Simon



More information about the Python-list mailing list