Please explain this for me

Noah Hall enalicho at gmail.com
Tue Dec 20 23:57:26 EST 2011


On Wed, Dec 21, 2011 at 4:39 AM, Emeka <emekamicro at gmail.com> wrote:
>
> Hello All,

> v = []
>
> def add_to_list(plist):
>     u = plist.append(90)
>     return u
>
> add_to_list(v)  # This function call returns nothing
> Could someone explain why this function call will return nothing?

It's because add_to_list returns the value returned from plist.append
stored in u.
append changes a list in place and returns nothing. Functions that
return nothing return None. This is why it'll be None - u is None
because append returns None.


> add_to_list([])
> This one returns nothing, why?

It's because the object [] here has no name, so that you have no way
to refer to it after the function changes it, since it changes it in
place. It gets eaten by Python, never to be seen again.



More information about the Python-list mailing list