islice ==> [::]

B

bearophileHUGS

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
 
R

Raymond Hettinger

[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
 
C

castironpi

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.
 
R

Raymond Hettinger

[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

_ ~
@ @
\_/
 
G

George Sakkis

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/Recipe/498272

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top