You could, but a key function is generally better, and the larger the
list the better (in the general case) it is likely to be. The problem is
that using __cmp__ requires the sort algorithm to make Python callbacks
from a C function for each object-pair comparison. Using the key
function, that fun
You could also achieve that using the __cmp__ magic method[1]:
class a:
def __init__(self, name, number):
self.name = name
self.number = number
def __cmp__(self, other):
return cmp(self.name, other.name)
def __repr__(self):
return "a(%s, %s)" % (rep
Ah, thanks!
/Nianbig
On 21 Sep, 19:45, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Nianbig wrote:
> > I have a people-list like this:
> class a:
> > ... def __init__(self, name, number):
> > ... self.name = name
> > ... self.number = number
> > ...
> b = []
Nianbig wrote:
> I have a people-list like this:
class a:
> ... def __init__(self, name, number):
> ... self.name = name
> ... self.number = number
> ...
b = []
b.append( a('Smith', 1) )
b.append( a('Dave', 456) )
b.append( a('Guran', 9432) )
>
Hi,
I have a people-list like this:
>>> class a:
... def __init__(self, name, number):
... self.name = name
... self.number = number
...
>>> b = []
>>> b.append( a('Smith', 1) )
>>> b.append( a('Dave', 456) )
>>> b.append( a('Guran', 9432) )
>>> b.append( a('Asdf', 12)
5 matches
Mail list logo