alwayssortedlist (was Re: On PEP 322 (ireverse))
Anton Vredegoor
anton at vredegoor.doge.nl
Thu Oct 30 10:12:44 EST 2003
Alex Martelli <aleax at aleax.it> wrote:
>class alwayssortedlist(list):
>
> def __init__(self, *args):
> list.__init__(self, *args)
> self.sort()
>
>We need to ensure sorting at creation. We _might_ make the sort
>method in this class a noop and call list.sort(self) instead,
>but, naah -- list.sort is SO fast when called on an already
>sorted list that it ain't even funny, so, choose simplicity.
>Onwards to simple mods:
>
> def append(self, *args):
> list.append(self, *args)
> self.sort()
>
> def __setitem__(self, *args):
> list.__setitem__(self, *args)
> self.sort()
How about this code:
a = alwaysssortedlist(range(10))
a[5] = 20
What's the use of assigning to a specific list position if the object
could end up being in some other place?
Anton
More information about the Python-list
mailing list