443,660 Members | 1,102 Online
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

# List mutation method gotcha - How well known?

 P: n/a Hi, I am surprised that it took me so long to bloody my nose on this one. It must be well known - and I would like to find out how well known. So here is a CLOSED BOOK multiple choice question - no RTFM, no playing at the interactive prompt: Given the following three lines of code at the interactive prompt: 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 I undertake to summarise answers posted to complete this "survey". - Hendrik Mar 13 '08 #1
14 Replies

 P: n/a On Mar 13, 8:36 am, "Hendrik van Rooyen"

 P: n/a On Mar 13, 9:36*am, "Hendrik van Rooyen"

 P: n/a Hendrik van Rooyen wrote: Hi, I am surprised that it took me so long to bloody my nose on this one. It must be well known - and I would like to find out how well known. So here is a CLOSED BOOK multiple choice question - no RTFM, no playing at the interactive prompt: Given the following three lines of code at the interactive prompt: 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 I undertake to summarise answers posted to complete this "survey". None, as python chose deliberately to return None on mutating functions like append, sort and reverse. Diez Mar 13 '08 #4

 P: n/a "Hendrik van Rooyen"

 P: n/a Hendrik van Rooyen a écrit : Hi, I am surprised that it took me so long to bloody my nose on this one. It must be well known - and I would like to find out how well known. So here is a CLOSED BOOK multiple choice question - no RTFM, no playing at the interactive prompt: Given the following three lines of code at the interactive prompt: 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 answer 5 - list.append returns None, which when printed gives 'None'. You'll get the same thing with list.sort, list.extend, list.reverse etc... Mar 13 '08 #6

 P: n/a Hendrik van Rooyen schreef: So here is a CLOSED BOOK multiple choice question - no RTFM, no playing at the interactive prompt: Given the following three lines of code at the interactive prompt: 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 Answer 5: the output will be 'None': append() doesn't return the list, it returns None. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven Mar 13 '08 #7

 P: n/a Paul Rubin

 P: n/a Still, I suppose this is a gotcha for a lot of people, just follow the good advice Paul said; "By Python convention, methods that mutate the object return None, and also stuff that returns None doesn't generate output at the interactive prompt." And you should survive most. Mar 13 '08 #9

 P: n/a On Mar 13, 2:36 am, "Hendrik van Rooyen"

 P: n/a On Mar 13, 2:36*pm, "Hendrik van Rooyen"

 P: n/a Dustan wrote: On Mar 13, 2:36 am, "Hendrik van Rooyen" Hi,I am surprised that it took me so long to bloody my nose on this one.It must be well known - and I would like to find out how well known.So here is a CLOSED BOOK multiple choice question - no RTFM,no playing at the interactive prompt:Given the following three lines of code at the interactive prompt:foo = [1,2,3,4]x = foo.append(5)print xWhat 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 tongue4) Nothing - no output5) None of the above 5. This will cause a hidden feature of python and the OS, known as the 'python easter egg', to activate - erasing all data on the hard disk and then reporting how many bytes of data are left. Usually "None" ;-} - This really is a 'gotcha'.... (Aren't you sorry you cheated and typed this in !!) So the answer is 5 ? Mar 13 '08 #12

 P: n/a On Mar 13, 10:42*am, Paul Rubin >l = [1, 2, 3]l.pop() 3 >>l [1, 2] >>> There is also the .next() method: >>i = iter([1, 2, 3])i.next() 1 >>i.next() 2 >>> I can't think of others ATM in python 2.x but there might be some. Moreover PEP 3119 introduces .add(), .discard(), .toggle() for MutableSets which all mutate self and return a non None object. -- Arnaud Mar 13 '08 #13

 P: n/a Chris wrote: No output because x is a NoneType... That's behavior of the interactive interpreter when printing results of expressions, not of print. It will print None. -- Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis There is no present or future; only the past, happening over and over again, now. -- Eugene O'Neill Mar 13 '08 #14

 P: n/a On Mar 13, 1:56 pm, yoz

### This discussion thread is closed

Replies have been disabled for this discussion.