bisect on a list of lists

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 9 20:41:42 EST 2007


En Fri, 09 Mar 2007 21:25:24 -0300, Paulo da Silva  
<psdasilvaX at esotericaX.ptX> escribió:

> Thanks Gabriel. This sounds very nice for my purpose.
> I have some doubts however. How do I "transform" a list into
> MyList? Is this the best way?
>
> class MyList(list):
> 	def __init__(self,l=None):
> 		if l != None:
> 			self=copy(l) (or self.extend(l) - this does not need copy module)
>
> Then I can do something like xl=MyList([ ... ])

Just omit the __init__ method, if you don't have anything additional to  
do. The inherited method will be used instead, as always:

py> class MyList(list):
...   def cdr(self):
...     return self[1:]
...
py> a = MyList([1,2,3])
py> a
[1, 2, 3]
py> type(a)
<class '__main__.MyList'>
py> a.cdr()
[2, 3]

> I read the description of __new__ but I didn't fully understand it and
> didn't find any examples on how it is used.
> Is it usable in this context? How?

Not really. Would be useful for a tuple (inmutable), because it has no  
__init__. But ask again when you really need it :)

-- 
Gabriel Genellina




More information about the Python-list mailing list