Suggestions for 2002
Andrew Dalke
dalke at dalkescientific.com
Sun Jan 13 00:35:19 EST 2002
Raymond Hettinger:
>Suggestion #2: Add new built-in functions: xmap, xfilter, and xzip.
>There is no reason why they can't be lazy also -- generating
>output only when needed, consuming inputs only when
>needed , and allowing for potentially infinite input streams.
I've found that lazy systems were harder to debug because,
amoung other reasons, it's impossible to print intermedate
structures.
a_list = xzip(range(26), string.letters)
print a_list
for a in a_list:
print a[0]+1, "-->", a[1]
I think a better solution for now is to have a 3rd party module
of lazy/iterator code. I'll contribute a few:
def nonnegatives():
i = 0
while 1:
yield i
i = i + 1
def positives():
i = 1
while 1:
yield i
i = i + 1
def negatives():
for i in positives():
yield -i
def integers():
yield 0
i = 1
while 1:
yield i
yield -i
i = i + 1
def mod_ring(n):
assert n > 0 and n == int(n), n
i = 0
while 1:
yield i
i = i + 1
if i >= n:
i = 0
>---------------
>Suggestion #3: Allow 'yield' with no return value.
Huh? So you want it to return a default value of None
like 'return' has? In parallel, I never have code which
intermixes 'return' and 'return value'. Similarly, I never
want to intermix 'yield' with 'yield value'. And if there
are no 'yield value' statements then the sequence returned
from the iterator is the very boring [None, None, None, ...]
Seems like you're using yield for a simple form of threading
or coroutines. I don't think I have enough experience with
yield for the purpose it was written to understand the usefulness
of variations like this.
The old-style solution, btw, is to write a new class to handle
the interactions you need. But I can't follow your code to
provide any suggestions.
>----------------
>Suggestion #4:
>
>Add a yield-like keyword 'accept' which suspends execution
>just like 'yield' but creates an object with two methods
>.submit() and .flush() which can take arguments and feed them
>into 'accept'. Flush returns and raises a StopStream exception.
You sure you don't want coroutines? One of the arguments against
a more complex iterator model (for now) was the inability to
support them on other platforms, like Jython. But I don't know
enough about this to comment wisely.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list