On Mon, Nov 14, 2011 at 2:16 AM, Ram Rachum <[email protected]> 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
_______________________________________________ Python-il mailing list [email protected] http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il
