bisect on a list of lists

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Fri Mar 9 21:45:50 EST 2007


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.

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

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'




-- 
Steven.




More information about the Python-list mailing list