Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List mutation method gotcha - How well known?

Reply
Thread Tools

List mutation method gotcha - How well known?

 
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      03-13-2008
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


 
Reply With Quote
 
 
 
 
cokofreedom@gmail.com
Guest
Posts: n/a
 
      03-13-2008
On Mar 13, 8:36 am, "Hendrik van Rooyen" <(E-Mail Removed)> 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".
>
> - Hendrik


None is the likely answer as .append is an inplace change and will
return None...
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-13-2008
Hendrik van Rooyen wrote:

> 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):


I thought it were a FAQ, but found only

http://effbot.org/pyfaq/why-doesn-t-...orted-list.htm

I'm sure you can draw the analogy.

Peter
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      03-13-2008
On Mar 13, 9:36*am, "Hendrik van Rooyen" <(E-Mail Removed)> 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".
>
> - Hendrik


No output because x is a NoneType...
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-13-2008
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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-13-2008
"Hendrik van Rooyen" <(E-Mail Removed)> writes:
> 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):
> 4) Nothing - no output


By Python convention, methods that mutate the object return None, and
also stuff that returns None doesn't generate output at the
interactive prompt.

There is a similar situation with list.sort() which led to the
introduction of the sorted() builtin.

Lately I try to avoid mutation, e.g. by using a generator or listcomp
instead of building up a list with .append()
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-13-2008
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...

 
Reply With Quote
 
Roel Schroeven
Guest
Posts: n/a
 
      03-13-2008
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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-13-2008
Paul Rubin <http://(E-Mail Removed)> writes:

> "Hendrik van Rooyen" <(E-Mail Removed)> writes:
> > 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):
> > 4) Nothing - no output


Correction, it will print None, there is an explicit print statement
that went past me. I'm sleepy.
 
Reply With Quote
 
cokofreedom@gmail.com
Guest
Posts: n/a
 
      03-13-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Data Mutation Program in java AnetaKvel Java 4 02-20-2007 09:04 AM
Password changing using mutation strings vysakhpillai@gmail.com C Programming 5 02-09-2006 04:00 PM
Self-Mutation with Strategy Object cppaddict Java 3 09-02-2004 09:12 PM
next iterator position without mutation Cagdas Ozgenc C++ 5 09-01-2003 09:50 AM



Advertisments