[Python-il] Self containment

Rani Hod rani.hod at gmail.com
Mon Nov 14 02:26:18 IST 2011


While preparing a home assignment for class. Neither fun nor pragmatic :p

On Mon, Nov 14, 2011 at 02:23, Ram Rachum <ram at rachum.com> wrote:

> I'm also curious to know whether you stumbled upon this while trying to do
> something pragmatic, or just trying to tear Python apart for fun :)
>
>
> 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?
>>
>>
>> Ram.
>>
>> On Mon, Nov 14, 2011 at 2:01 AM, Rani Hod <rani.hod at gmail.com> wrote:
>>
>>> Dear Abby,
>>>
>>> Any idea why sorted and list.sort behave differently in the following
>>> example?
>>> (specifically, why x is not sorted in the end?)
>>>
>>> Thanks,
>>> R.
>>>
>>> ------------8<--------------------8<--------
>>> >>> x = ['one','two','three']; x.append(x)
>>> >>> sorted(x, key=len)
>>> ['one', 'two', ['one', 'two', 'three', [...]], 'three']
>>> >>> x.sort(key=len); x
>>> [[...], 'one', 'two', 'three']
>>> ------------8<--------------------8<--------
>>>
>>> _______________________________________________
>>> Python-il mailing list
>>> Python-il at hamakor.org.il
>>> http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://hamakor.org.il/pipermail/python-il/attachments/20111114/7ee10a52/attachment.htm>


More information about the Python-il mailing list