Alternative iterator syntax

Aahz Maruch aahz at panix.com
Wed Feb 21 11:15:02 EST 2001


In article <mailman.982769240.31345.python-list at python.org>,
Huaiyu Zhu  <hzhu at users.sourceforge.net> wrote:
>
>Following suggestion by Jeff Petkau <jpet at eskimo.com>,
>(http://mail.python.org/pipermail/python-list/2001-February/029944.html),
>here is a different proposal for iterators.

This is really great; with a tiny bit of work, what you're calling an
iterator is almost the same as an Icon generator, something many of us
have been wishing for a long time.  I hope you cc'd this to Ka-Ping; I'd
also suggest posting this to python-dev.

>Applications to Builtins and Other Common Situations:
>
>1. Sequences.  The builtin sequence types (list, tuple, string) are
>  changed to iterators by adding the three magic methods __next__,
>  __contains__ and __list__.  The following is always true:
>
>    x.__list__() == list(x)
>
>  In addition, Each sequence object x defines two attributes (indexes,
>  items) that are themselves iterators.  The arity of indexes is 1, that
>  of items is 2.
>
>    x.indexes.__list__() == range(len(x))
>    x.items.__list__() == zip(range(len(x)), x)

I think I would change the text to read:

    In addition, each sequence object x defines two methods (indexes,
    items) that each return an iterator object.  The arity of indexes is
    1, that of items is 2.

The reason is that we want to keep hammering on the generality of
iterators, for example in passing a single iterator object to a bunch of
threads to generate keys.

That brings up the point of how iterator.__list__() should work with an
unbounded iterator.

>Both UnpackError and ArityError are subclasses of ValueError.  This
>could be implemented this way: If the left hand side of an assignment is
>a tuple while the right hand side is a call to f, check that the arity
>of the tuple matches f.__arity__ unless f.__arity__ is None.
>
>All functions not defining __arity__ attribute default to
>__arity__==None.  Therefore one can write things like
>
>    if f.__arity__ == 1: x = f()
>    elif f.__arity__ == 2: x,y = f()
>    else: items = f()

I have no clue how to design/implement this, but it would be really neat
if functions could *read* the desired arity and change the return value
based on it.
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

The problem with an ever-changing .sig is that you have to keep changing it



More information about the Python-list mailing list