[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

islice ==> [::]

Bearophile

3/7/2008 12:49:00 PM

I find itertools.islice() useful, so for Python 3.x I may like to see
it removed from the itertools module, and the normal slicing syntax
[::] extended to work with generators/iterators too.

from itertools import islice
primes = (x for x in xrange(1,999) if all(x % y for y in xrange(2,
x)))
print list(islice(primes, 0, 20))
==>
print list(primes[:20])

Bye,
bearophile
5 Answers

Raymond Hettinger

3/7/2008 7:15:00 PM

0

[bearophileH]
> I find itertools.islice() useful, so for Python 3.x I may like to see
> it removed from the itertools module, and the normal slicing syntax
> [::] extended to work with generators/iterators too.

This is a can of worms. First, remember iterations is a protocol, not
a type. So, this would have to be added to every possible iterator
class (dict.iteritems() and enumerate() for example). Second, he who
wants slicing, at some time will want getitem, but Guido ruled this
out long ago saying that it is a mistake to conflate sequences and
general iterables. Third, the analogy breaks down quickly (i.e.
chain(it[:2], it[2:]) does not give the same result as iter(it) unless
working on a re-iterable sequence). Fourth, this suggests other
related hyper-generalizations which also break down in practice (i.e.
using the plus operator for chain() breaks down when you write it+it
and find that the second one is fully consumed by the time chain()
gets to it).

Besides, if you know what you're doing it is simple to write a trivial
wrapper class that temporarily supports the slicing notation:

class W:
def __init__(self, it):
self.it = iter(it)
def __getitem__(self, n):
if isinstance(n, slice):
return islice(self.it, n.start, n.stop, n.step)
return islice(self.it, n, n+1)

>>> s = 'abcdefg'
>>> list(W(s)[2:])
['c', 'd', 'e', 'f', 'g']
>>> list(W(s)[:2])
['a', 'b']
>>> list(W(s)[::2])
['a', 'c', 'e', 'g']
>>> list(W(s)[2])
['c']



Raymond

Aaron Brady

3/7/2008 7:23:00 PM

0

> > I find itertools.islice() useful, so for Python 3.x I may like to see
> general iterables.  Third, the analogy breaks down quickly (i.e.
> chain(it[:2], it[2:]) does not give the same result as iter(it) unless

> >>> s = 'abcdefg'
> >>> list(W(s)[2:])

Slice literals are a logical next step, precedented by raw strings and
bytes. slice= islice is too, precedented by range= xrange.

Does s[2:] evaluate to an infinity? What is the natural meaning of
skipping finitely many terms at the head of an iterable? itertools
can also grow 'skip(n=0)' and 'drop(step=3)' operations.

Raymond Hettinger

3/7/2008 7:55:00 PM

0

[castiro]
> Slice literals are a logical next step, precedented by raw strings and
> bytes. slice= islice is too, precedented by range= xrange.

Looking closely at the [::] notation, I think it can easily be
confused with an open box of fleas. IMO, the one unequivocal,
explicit way of checking for lice is itertools.is_lice().


Raymond

_ ~
@ @
\_/

George Sakkis

3/8/2008 2:55:00 AM

0

On Mar 7, 7:49 am, bearophileH...@lycos.com wrote:
> I find itertools.islice() useful, so for Python 3.x I may like to see
> it removed from the itertools module, and the normal slicing syntax
> [::] extended to work with generators/iterators too.
>
> from itertools import islice
> primes = (x for x in xrange(1,999) if all(x % y for y in xrange(2,
> x)))
> print list(islice(primes, 0, 20))
> ==>
> print list(primes[:20])
>
> Bye,
> bearophile

I had posted some time ago an OO wrapper of itertools; slice notation
for islice was one motivation: http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...

George

Bearophile

3/8/2008 12:00:00 PM

0

George Sakkis:
> I had posted some time ago an OO wrapper of itertools; slice notation
> for islice was one motivation:http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...

I think I did see that, but later I forgot of it. It looks nice.
Thank you,
bearophile