Strange behavior with sort()

2014-02-26 Thread ast

Hello

box is a list of 3 integer items

If I write:

   box.sort()
   if box == [1, 2, 3]:


the program works as expected. But if I write:

   if box.sort() == [1, 2, 3]:

it doesn't work, the test always fails. Why ?

Thx
--
https://mail.python.org/mailman/listinfo/python-list


Re: Strange behavior with sort()

2014-02-26 Thread Frank Millman

ast nom...@invalid.com wrote in message 
news:530eda1d$0$2061$426a7...@news.free.fr...
 Hello

 box is a list of 3 integer items

 If I write:

box.sort()
if box == [1, 2, 3]:


 the program works as expected. But if I write:

if box.sort() == [1, 2, 3]:

 it doesn't work, the test always fails. Why ?


Try the following in the interpreter -

 box = [3, 2, 1]
 box.sort()
 box
[1, 2, 3]

 box = [3, 2, 1]
print(box.sort())
None
 box
[1, 2, 3]

box.sort() sorts box 'in situ', but does not return anything. That is why 
the second example prints None.

In your second example, you are comparing the return value of box.sort() 
with [1, 2, 3]. As the return value is None, they are unequal.

HTH

Frank Millman



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


Re: Strange behavior with sort()

2014-02-26 Thread Eduardo A . Bustamante López
On Thu, Feb 27, 2014 at 07:24:24AM +0100, ast wrote:
 Hello
 
 box is a list of 3 integer items
 
 If I write:
 
box.sort()
if box == [1, 2, 3]:
 
 
 the program works as expected. But if I write:
 
if box.sort() == [1, 2, 3]:
 
 it doesn't work, the test always fails. Why ?
 
 Thx
 -- 
 https://mail.python.org/mailman/listinfo/python-list

Because when you call the .sort() method on a list, it does the sort
in-place, instead of returning a sorted copy of the list. Check this:

 [2,1,3].sort()
 

The method does not return a value, that's why the direct comparison
fails.

What you might want is to use the sorted() method on the list, like
this:

 sorted([2,1,3])
[1, 2, 3]
 sorted([2,1,3]) == [1,2,3]
True

-- 
Eduardo Alan Bustamante López
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Strange behavior with sort()

2014-02-26 Thread Lele Gaifax
ast nom...@invalid.com writes:

 If I write:

box.sort()
if box == [1, 2, 3]:

 the program works as expected. But if I write:

if box.sort() == [1, 2, 3]:

 it doesn't work, the test always fails. Why ?

Because very often methods **dont't** return the object they are applied
(self that is).

This works though:

 box = [1,3,2]
 sorted(box) == [1,2,3]
True

hth, ciao, lele.
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: Strange behavior with sort()

2014-02-26 Thread Marko Rauhamaa
ast nom...@invalid.com:

 if I write:

if box.sort() == [1, 2, 3]:

 it doesn't work, the test always fails. Why ?

The list.sort() method returns None.

The builtin sorted() function returns a list:

   if sorted(box) == [1, 2, 3]:

would work.

Note that the list.sort() method is often preferred because it sorts the
list in place while the sorted() function must generate a fresh, sorted
list.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Strange behavior with sort()

2014-02-26 Thread ast

Thanks for the very clear explanation
--
https://mail.python.org/mailman/listinfo/python-list


Re: Strange behavior with sort()

2014-02-26 Thread Gary Herron

On 02/26/2014 10:24 PM, ast wrote:

Hello

box is a list of 3 integer items

If I write:

   box.sort()
   if box == [1, 2, 3]:


the program works as expected. But if I write:

   if box.sort() == [1, 2, 3]:


Most such questions can be answered by printing out the values in 
question and observing first hand what the value is.


So, print out box.sort() to see what it is.  You might be surprised.

Hint: box.sort() does indeed cause box to be sorted, and the sorted list 
is left in box, but the sorted list is not returned as a function value.




it doesn't work, the test always fails. Why ?

Thx


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


Re: Strange behavior with sort()

2014-02-26 Thread Larry Hudson

On 02/26/2014 10:24 PM, ast wrote:

Hello

box is a list of 3 integer items

If I write:

box.sort()
if box == [1, 2, 3]:


the program works as expected. But if I write:

if box.sort() == [1, 2, 3]:

it doesn't work, the test always fails. Why ?

Thx


sort() sorts the sequence in place, but it _returns_ None.
Your second example becomes the equivalent of:

box.sort()
if None == [1, 2, 3]:

So although your box does become sorted, it is NOT what is compared in your if 
statement.

BTW, the sorted() function won't work here either.  It will return the sorted sequence, but it 
leaves the original unchanged.


 -=- Larry -=-

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