Re: List mutation method gotcha - How well known?

2008-03-16 Thread Lie
On Mar 15, 1:01 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Fri, 14 Mar 2008 11:32:41 -0700, Lie wrote:
> > 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.

I personally won't to rely on it. Sometimes a return statement might
be documented, but not complete enough, like this:

def genericadd(a, b):
"""
Adds two number, returns the same as longadd if
either operands are long type and returns the same
as intadd if all operands are int type.

"""
if isinstance(a, long) or isinstance(b, long):
return longadd(a, b)
if isinstance(a, int) and isinstance(b, int):
return intadd(a, b)
return "Error"

This function is written badly on purpose to simulate what a less-than-
intelligent programmer might write, which is in no control of ours as
a class user.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-15 Thread Hendrik van Rooyen
On Thu, Mar 13, 2008 at 8:36 AM, Hendrik van Rooyen
<[EMAIL PROTECTED]> 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
None
>>> 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
could
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





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-14 Thread Marc 'BlackJack' Rintsch
On Fri, 14 Mar 2008 11:32:41 -0700, Lie wrote:

> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-14 Thread Lie
On Mar 14, 4:57 pm, Jarek Zgoda <[EMAIL PROTECTED]> wrote:
> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-14 Thread Dustan
On Mar 13, 1:56 pm, yoz <[EMAIL PROTECTED]> wrote:
> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-14 Thread 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.

-- 
Jarek Zgoda
Skype: jzgoda | GTalk: [EMAIL PROTECTED] | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Erik Max Francis
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 && [EMAIL PROTECTED] && 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread John Machin
On Mar 14, 6:13 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
> On Mar 13, 10:42 am, Paul Rubin  wrote:
> [...]
>
> > 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Arnaud Delobelle
On Mar 13, 10:42 am, Paul Rubin  wrote:
[...]
> 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.

--
Arnaud

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread yoz
Dustan wrote:
> On Mar 13, 2:36 am, "Hendrik van Rooyen" <[EMAIL PROTECTED]> 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
> 
> 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 ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Lie
On Mar 13, 2:36 pm, "Hendrik van Rooyen" <[EMAIL PROTECTED]> 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

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)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread BJörn Lindqvist
On Thu, Mar 13, 2008 at 8:36 AM, Hendrik van Rooyen
<[EMAIL PROTECTED]> 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

5


-- 
mvh Björn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Dustan
On Mar 13, 2:36 am, "Hendrik van Rooyen" <[EMAIL PROTECTED]> 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

5.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Paul Rubin
Paul Rubin  writes:

> "Hendrik van Rooyen" <[EMAIL PROTECTED]> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread 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...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Paul Rubin
"Hendrik van Rooyen" <[EMAIL PROTECTED]> 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()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Diez B. Roggisch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Chris
On Mar 13, 9:36 am, "Hendrik van Rooyen" <[EMAIL PROTECTED]> 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...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread Peter Otten
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-list-sort-return-the-sorted-list.htm

I'm sure you can draw the analogy.

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List mutation method gotcha - How well known?

2008-03-13 Thread cokofreedom
On Mar 13, 8:36 am, "Hendrik van Rooyen" <[EMAIL PROTECTED]> 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...
-- 
http://mail.python.org/mailman/listinfo/python-list


List mutation method gotcha - How well known?

2008-03-13 Thread 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


-- 
http://mail.python.org/mailman/listinfo/python-list