Strange behavior with sort()

Lele Gaifax lele at metapensiero.it
Thu Feb 27 01:41:03 EST 2014


"ast" <nomail at 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.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list