append method

Karl Knechtel zahlman at gmail.com
Wed May 23 15:10:08 EDT 2012


On Wed, May 23, 2012 at 8:23 AM, 水静流深 <1248283536 at qq.com> wrote:
>>>> s=[1,2,3]
>>>> s.append(5)
>>>> s
> [1, 2, 3, 5]
>>>> s=s.append(5)
>>>> s
>>>> print s
> None
>
> why can't  s=s.append(5)  ,what is the reason?

For the same reason that you don't see `[1, 2, 3, 5]` immediately
after doing `s.append(5)` the first time around, but must instead
check `s`: because the value is not returned from the function. `s` is
modified in-place, and nothing is returned.

This was a deliberate design decision made a long time ago that is
very well documented; try Googling for `python why doesn't list.append
return the value` for example.


-- 
~Zahlman {:>



More information about the Python-list mailing list