[Python-3000] Builtin iterator type

George Sakkis george.sakkis at gmail.com
Wed Nov 15 01:23:24 CET 2006


On 11/14/06, Mike Klaas <mike.klaas at gmail.com> wrote:

> On 11/14/06, George Sakkis <george.sakkis at gmail.com> wrote:
> > On 11/14/06, Mike Klaas <mike.klaas at gmail.com> wrote:
>
> > > I don't see the problem of importing important language functionality.
> > >  Most languages since c have required somthing similar.
> >
> > Most languages since C are not as readable and elegant as Python. I
> > think we all agree that list1[5:] + list2 is much better than the
> > hypothetical
> >     list1.getslice(5,None).concatenate(list2)
> > or
> >     from sequenceutils import concatenate, getslice
> >     concatenate(getslice(list1,5,None), list2)
> >
> > Unfortunately, people here seem to consider natural the latter when it
> > comes to itertools. Oh well, whatever.
>
> Two differences:
>    - slicing an iterator is a rare activity; slicing a list is common

Slicing an infinite iterator is *very* common, to which you'll
probably reply that infinite iterators are rare. I guess neither of us
has numbers to support his claim, so let's leave it at that.

>    - I often use .extend rather than += when appending iterables to lists
>
> Finally, your analogy between sequences and iterators is flawed.  +
> does not work for sequences, but only for concrete types:
>
> >>> [8] + (9,)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: can only concatenate list (not "tuple") to list
>
> In python, only concrete types tend to implement syntatic operator
> support (another example: sets only support +, -, |, & with other
> sets, but their methods accept iterables).
>
> -Mike
>

The TypeError is not raised because the arguments are not of a
concrete type (all python types are concrete; interfaces or protocols
are informal descriptions) but because they're not the *same* type,
and I don't object to this. This is not the case in what I propose: in
the expression Iter(file('foo')) + Iter(some_string), both operands
have the same type, the Iter wrapper that wraps an underlying
iterator.

George


More information about the Python-3000 mailing list