list.sort, was Re: [Python-Dev] decorate-sort-undecorate
Bob Ippolito
bob at redivi.com
Mon Oct 27 22:14:06 EST 2003
> [Gustavo Niemeyer wrote]
> > > You can do reverse with [::-1] now.
>
> [Holger Krekel]
> > sure, but it's a bit unintuitive and i mentioned not only reverse :-)
> >
> > Actually i think that 'reverse', 'sort' and 'extend' algorithms
> > could nicely be put into the new itertools module.
> >
> > There it's obvious that they wouldn't mutate objects. And these
> > algorithms
> > (especially extend and reverse) would be very efficient as iterators
> > because
> > they wouldn't create temporary lists/tuples.
>
> To be considered as a possible itertool, an ideal candidate should:
>
> * work well in combination with other itertools
> * be a fundamental building block
> * accept all iterables as inputs
> * return only an iterator as an output
> * run lazily so as not to force the inputs to run to completion
> unless externally requested by list() or some such.
> * consume constant memory (this rule was bent for itertools.cycle(),
> but should be followed as much as possible).
> * run finitely if some of the inputs are finite (itertools.repeat(),
> count() and cycle() are the only intentionally infinite tools)
>
> There is no chance for isort(). Once you've sorted the whole list,
> there is no advantage to returning an iterator instead of a list.
>
> The problem with ireverse() is that it only works with objects that
> support __getitem__() and len(). That pretty much precludes
> generators, user defined class based iterators, and the outputs
> from other itertools. So, while it may make a great builtin (which
> is what PEP-322 is going to propose), it doesn't fit in with other
> itertools.
How about making islice be more lenient about inputs? For example
x[::-1] should be expressable by islice(x, None, None, -1) when the
input implements __len__ and __getitem__ -- but it's not. [::-1]
*does* create a temporary list, because Python doesn't have "views" of
lists/tuples. islice should also let you go backwards in general,
islice(x, len(x)-1, None, -2) should work.
-bob
More information about the Python-Dev
mailing list