List mutation method gotcha - How well known?

H

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 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
 
C

cokofreedom

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...
 
P

Peter Otten

Hendrik said:
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-list-sort-return-the-sorted-list.htm

I'm sure you can draw the analogy.

Peter
 
C

Chris

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...
 
D

Diez B. Roggisch

Hendrik said:
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
 
P

Paul Rubin

Hendrik van Rooyen said:
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()
 
B

Bruno Desthuilliers

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...
 
R

Roel Schroeven

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
 
P

Paul Rubin

Paul Rubin said:
Hendrik van Rooyen said:
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.
 
C

cokofreedom

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.
 
D

Dustan

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

5.
 
L

Lie

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

I think I'll choose 3. Well, no, I suppose the correct behavior
_should_ be undefined (i.e. what it returns is an implementation
details that should not be relied on). The fact that it returns None
is just a "coincidence" that happens to happen every time you tested
it (you can't prove by ignorance)
 
Y

yoz

Dustan said:
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

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 ?
 
A

Arnaud Delobelle

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

A convention that does not always hold:
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.
 
J

John Machin

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

A convention that does not always hold:


l = [1, 2, 3]
l.pop() 3
l
[1, 2]

Try this then for the "convention": Any function/method that is not
documented to return something else should be assumed to return None.
Note: no nexus with whether or not the function/method mutates its
args.
 
E

Erik Max Francis

Chris said:
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.
 
J

Jarek Zgoda

Lie napisa³(a):
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".

I think I'll choose 3. Well, no, I suppose the correct behavior
_should_ be undefined (i.e. what it returns is an implementation
details that should not be relied on). The fact that it returns None
is just a "coincidence" that happens to happen every time you tested
it (you can't prove by ignorance)

I think in Python there's no notion of "void" return type. Deliberate
choice to return None for functions that modify objects in place seems
to be OK as long as it is used consistently and documented. Which is the
case.
 
D

Dustan

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 ?

Good one. You got a smile out of me.
 
L

Lie

Lie napisa³(a):


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".
I think I'll choose 3. Well, no, I suppose the correct behavior
_should_ be undefined (i.e. what it returns is an implementation
details that should not be relied on). The fact that it returns None
is just a "coincidence" that happens to happen every time you tested
it (you can't prove by ignorance)

I think in Python there's no notion of "void" return type. Deliberate
choice to return None for functions that modify objects in place seems
to be OK as long as it is used consistently and documented. Which is the
case.

No, there is no need for "void" return type, what I meant is that
everything that's not said in the documentation should be assumed to
be an implementation detail, a method or a function that doesn't say
anything about its return type should be assumed to return an
implementation detail (which basically means: Don't rely on this). The
fact that list.append returns none is just a coincidence, you might
encounter another list.append that returns different thing some time
in the future, or the far future, or perhaps at the end of the
galaxy's life.
 
M

Marc 'BlackJack' Rintsch

No, there is no need for "void" return type, what I meant is that
everything that's not said in the documentation should be assumed to
be an implementation detail, a method or a function that doesn't say
anything about its return type should be assumed to return an
implementation detail (which basically means: Don't rely on this). The
fact that list.append returns none is just a coincidence, you might
encounter another list.append that returns different thing some time
in the future, or the far future, or perhaps at the end of the
galaxy's life.

I expect functions with no documentation of what they return to return
`None`. Assuming they are documented at all, of course. :)

It's like not writing a ``return`` statement in the code: there's always an
implicit ``return None`` at the end of every function.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top