[Tutor] assignment question

Michael H. Goldwasser goldwamh at slu.edu
Mon Nov 12 05:34:26 CET 2007


On Sunday November 11, 2007, Ryan Hughes wrote: 

>    Hello,
>    
>    Why does the following not return [1,2,3,4] ?
>    
>    >>> x = [1,2,3].append(4)
>    >>> print x
>    None


The reason is that the append method does not return anything.  In
effect, the expresison [1,2,3].append(4) temporarily causes 4 to be
appended to the (unnamed) list.  However the assignment

x = blah

sets the variable x equal to the result of the expression blah (which
in this case is None, since append does not return anything).

In contrast, consider the following interaction:

>>> x = [1,2,3]
>>> x.append(4)
>>> print x
[1, 2, 3, 4]






More information about the Tutor mailing list