Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Amit Aronovitch <aronovitch <at> gmail.com>
Subject: Re: Self containment
Newsgroups: gmane.comp.python.israel
Date: Monday 14th November 2011 07:19:27 UTC (over 6 years ago)
On Mon, Nov 14, 2011 at 2:16 AM, Ram Rachum  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
 
CD: 4ms