bisect on a list of lists

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Mar 10 04:40:24 CET 2007


En Fri, 09 Mar 2007 23:45:50 -0300, Steven D'Aprano  
<steve at REMOVE.THIS.cybersource.com.au> escribió:

> On Fri, 09 Mar 2007 18:57:42 -0300, Gabriel Genellina wrote:
>
>> En Fri, 09 Mar 2007 17:15:44 -0300, Paulo da Silva
>> <psdasilvaX at esotericaX.ptX> escribió:
>>
>>> What is the best way to have something like the bisect_left
>>> method on a list of lists being the comparision based on an
>>> specified arbitrary i_th element of each list element?
>>>
>>> Is there, for lists, something equivalent to the __cmp__ function for
>>> classes?
>>
>> lists *are* classes (at least since Python 2.2)
>
> Not quite. Classes and types are *almost* the same, but not exactly.

I were talking about new style classes, that's why I said Python 2.2

>>>> type(list)
> <type 'type'>
>>>> class Spam:
> ...     pass
> ...
>>>> type(Spam)
> <type 'classobj'>

py> class New(object): pass
...
py> type(New)
<type 'type'>

New-style clases are instances of type too (like list, tuple and other  
builtins), and subclasses of object:

py> New.mro()
[<class '__main__.New'>, <type 'object'>]
py> list.mro()
[<type 'list'>, <type 'object'>]

> Usually the difference doesn't make a difference, but sometimes it does.
>
>>>> Spam.x = 3
>>>> list.x = 3
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: can't set attributes of built-in/extension type 'list'

That's not because list is a builtin type, but because it lacks (by  
design) any means to store instance attributes (it has an empty  
tp_dictoffset slot, by example).
Functions, exceptions, modules are examples of builtin types whose  
instances can handle attributes.

-- 
Gabriel Genellina




More information about the Python-list mailing list