List mutation method gotcha - How well known?

Hendrik van Rooyen mail at
Sat Mar 15 09:59:44 CET 2008

On Thu, Mar 13, 2008 at 8:36 AM, Hendrik van Rooyen
<m.. at> wrote:
>  foo = [1,2,3,4]
>  x = foo.append(5)
>  print x
>  What will be the output (choose one):
>  1)  [1,2,3,4]
>  2)  [1,2,3,4,5]
>  3)  That famous picture of Albert Einstein sticking out his tongue
>  4)  Nothing - no output
>  5)  None of the above

windows using idle:

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

So the "Correct" answer is 5, and I would have allowed 4 also if it were a test,
which it wasn't.

I count ten responses, but this is probably wrong, as I did not receive digest
number 171 which must have contained my original post.  All of them seemed
aware of the fact the append returned None.

In any case, none of the respondents fell for the gotcha - answer 2, that
assumes that the
append returns the mutated list.

One respondent wanted to choose the picture, but changed his mind.

This leads me to conclude that this behaviour is well known amongst people in
this group.

Now this need not necessarily be correct either, as people who were uncertain
simply have declined to respond, or they were so honest that after checking,
they found
themselves morally unable to respond as that would have been cheating...   : - )

I am constantly amazed and delighted by how well Python fits my brain - its the
easiest assembler
language I have ever used - almost everything "just works".

This is true even if like me, you don't read the manual, but blunder your way
along, guessing as you go -
most of the time the guesses are right, and if they are not, a few minutes at
the interactive prompt serves to
dispel most illusions.

Thanks to everybody who responded.

- Hendrik

More information about the Python-list mailing list