[Python-il] Self containment
Amit Aronovitch
aronovitch at gmail.com
Mon Nov 14 09:19:27 IST 2011
On Mon, Nov 14, 2011 at 2:16 AM, Ram Rachum <ram at rachum.com> wrote:
> Very interesting Rani!
>
> I investigated this by making a replacement of the `len` function and
> putting breakpoints inside of it.
>
> The answer: When you call `list.sort`, it first empties the list, and then
> starts measuring the length of the items for sorting. So when measuring the
> list itself, it gets a result of 0 because the list has been emptied.
>
> The question is: Is there a good reason for Python behaving like that?
>
>
Use the source, Luke...
1916<http://hg.python.org/cpython/file/410115400838/Objects/listobject.c#l1916>
/*
The list is temporarily made empty, so that mutations performed
1917<http://hg.python.org/cpython/file/410115400838/Objects/listobject.c#l1917>
*
by comparison functions can't affect the slice of memory we're
1918<http://hg.python.org/cpython/file/410115400838/Objects/listobject.c#l1918>
*
sorting (allowing mutations during sorting is a core-dump
1919<http://hg.python.org/cpython/file/410115400838/Objects/listobject.c#l1919>
*
factory, since ob_item may change).
1920<http://hg.python.org/cpython/file/410115400838/Objects/listobject.c#l1920>
*/
(Probably to avoid crashing when smartasses like us play around with
Escheresque lists)
See also
http://hg.python.org/cpython/file/410115400838/Objects/listsort.txt
AA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://hamakor.org.il/pipermail/python-il/attachments/20111114/b4544c55/attachment.htm>
More information about the Python-il
mailing list