| Store | Cart

Re: generator/coroutine terminology

From: Steven DAprano <stev...@pearwood.info>
Mon, 16 Mar 2015 19:36:50 +1100
On Monday 16 March 2015 18:12, Marko Rauhamaa wrote:

> Steven D'Aprano <stev...@pearwood.info>:> >> Marko Rauhamaa wrote:>>> What features do generator iterators provide on top of generic>>> iterators?>>>> The biggest difference is syntactic. Here's an iterator which returns a>> never-ending sequence of squared numbers 1, 4, 9, 16, ...>>>> class Squares:>>     def __init__(self):>>         self.i = 0>>     def __next__(self):>>         self.i += 1>>         return self.i**2>>     def __iter__(self):>>         return self>>>>>> Here's the same thing written as a generator:>>>> def squares():>>     i = 1>>     while True:>>         yield i**2>>         i += 1> > I was actually referring to the offered API. It still seems to me like> all iterators could offer close(), send() and throw(). Why? To make all> iterators drop-in replacements of each other.
How could these two iterators *possibly* be drop in replacements for each 
other in any real body of code?


def inorder(tree):
    for node in inorder(tree.left):
        yield node.payload
    yield tree.payload
    for node in inorder(tree.right):
        yield node.payload


def clean_up(collection, condition, cleaner=None):
    if cleaner is None:
        def cleaner(obj):
            try:
                obj.cache = {}
                obj.ref = None
            except Exception:
                return False
            return True
    for i, item in enumerate(collection):
        if condition(item):
            flag = cleaner(item)
            if flag:
                yield Warn(MSG % (i, item))


You can't just substitute any random iterator for another, any more than you 
can substitute any random two functions for each other.

Overly pure (in the comp sci theory sense) interfaces can be silly. Just 
because the Soyez rocket and my nephew's tricycle are both instances of 
Vehicle doesn't mean that I should be able to substitute one for the other. 
OOP theory says they should. Reality says No Way.

Why should I, the class designer, be forced to offer send(), throw() and 
close() methods on my iterators if I'm never going to use them as 
coroutines? If I .send() into either of the generators above, its a 
conceptual mistake and I should get an error. The fact that I don't is an 
implementation detail, and one which hopefully will be fixed. I expect that 
the interpreter can tell the difference between 

    yield spam

and 

    x = yield spam

and only allow send(), close() and throw() on generators which include the 
second form. If it can't, then that's a limitation, not a feature to be 
emulated.


> Then, you could also stop wondering what to call the thingy returned by> a generator. 
Apart from a few die-hards who are hoping for some sort of Académie 
Française to define the One True meaning of words, most of us stopped 
wondering what to call the thingy returned by a generator function years 
ago. It's a generator, just like introspection of the object tells you.


> Why, it would be an iterator. You wouldn't then have any> other use for a generator than the function that returns an iterator.
Lots of things are iterators. Doesn't make them generators.


> You could then decide if you want to reserve the name "generator" for> functions that contain a yield statement (syntactic notion) or call any> function returning an iterator a "generator" (semantic notion).
Why would you want the second? Do you have special names for {all functions 
which return strings} and {all functions which return bools}?

Functions are normally described by their *form* or *purpose*, not their 
return result, e.g. inner function, public function, callback function, 
reentrant function, filter function.


-- Steve

-- 
https://mail.python.org/mailman/listinfo/python-list
Recent Messages in this Thread
Rustom Mody Mar 12, 2015 01:35 pm
Chris Angelico Mar 12, 2015 01:55 pm
brea...@gmail.com Mar 12, 2015 01:57 pm
Steven DAprano Mar 12, 2015 04:27 pm
Rustom Mody Mar 12, 2015 04:52 pm
Marko Rauhamaa Mar 12, 2015 05:55 pm
Rustom Mody Mar 13, 2015 02:23 am
Steven DAprano Mar 13, 2015 03:30 am
Rustom Mody Mar 13, 2015 05:28 am
Chris Angelico Mar 13, 2015 08:23 am
Rustom Mody Mar 13, 2015 09:12 am
Marko Rauhamaa Mar 13, 2015 09:36 am
Steven DAprano Mar 14, 2015 06:04 am
Marko Rauhamaa Mar 14, 2015 07:54 am
Mark Lawrence Mar 14, 2015 08:04 am
Ian Kelly Mar 14, 2015 08:14 pm
Mark Lawrence Mar 14, 2015 08:31 pm
Rustom Mody Mar 15, 2015 04:15 am
Marko Rauhamaa Mar 14, 2015 08:30 am
Rustom Mody Mar 14, 2015 03:29 pm
Chris Angelico Mar 14, 2015 03:56 pm
Rustom Mody Mar 14, 2015 03:59 pm
Chris Angelico Mar 14, 2015 04:14 pm
Rustom Mody Mar 14, 2015 04:33 pm
Chris Angelico Mar 14, 2015 04:51 pm
Dave Angel Mar 14, 2015 05:07 pm
Mark Lawrence Mar 14, 2015 04:56 pm
Rustom Mody Mar 14, 2015 05:17 pm
Steven DAprano Mar 15, 2015 08:37 am
Chris Angelico Mar 13, 2015 11:32 am
Oscar Benjamin Mar 14, 2015 10:02 pm
Marko Rauhamaa Mar 14, 2015 10:15 pm
Chris Angelico Mar 14, 2015 10:24 pm
Marko Rauhamaa Mar 15, 2015 12:15 am
Chris Angelico Mar 15, 2015 12:22 am
Steven DAprano Mar 16, 2015 01:03 am
Marko Rauhamaa Mar 16, 2015 07:12 am
Chris Angelico Mar 16, 2015 07:21 am
Ian Kelly Mar 16, 2015 07:37 am
Steven DAprano Mar 16, 2015 08:36 am
Chris Angelico Mar 16, 2015 08:58 am
Marko Rauhamaa Mar 16, 2015 12:32 pm
Rustom Mody Mar 16, 2015 12:51 pm
Marko Rauhamaa Mar 16, 2015 01:13 pm
Steven DAprano Mar 16, 2015 02:32 pm
Ian Kelly Mar 16, 2015 02:45 pm
Steven DAprano Mar 16, 2015 01:39 pm
Rustom Mody Mar 16, 2015 02:19 pm
Mark Lawrence Mar 16, 2015 02:26 pm
Steven DAprano Mar 16, 2015 02:35 pm
Steven DAprano Mar 16, 2015 02:36 pm
Rustom Mody Mar 16, 2015 02:37 pm
Rustom Mody Mar 16, 2015 02:55 pm
Mark Lawrence Mar 16, 2015 06:19 pm
Rustom Mody Mar 17, 2015 02:52 am
Mark Lawrence Mar 17, 2015 03:07 am
Rustom Mody Mar 17, 2015 03:18 am
Ian Kelly Mar 16, 2015 02:52 pm
Marko Rauhamaa Mar 16, 2015 03:09 pm
Ian Kelly Mar 16, 2015 03:26 pm
Marko Rauhamaa Mar 16, 2015 04:05 pm
Steven DAprano Mar 16, 2015 11:51 am
Chris Angelico Mar 16, 2015 01:16 pm
Marko Rauhamaa Mar 16, 2015 07:52 am
Steven DAprano Mar 16, 2015 12:02 pm
Jonas Wielicki Mar 16, 2015 12:39 pm
Marko Rauhamaa Mar 16, 2015 12:42 pm
Marko Rauhamaa Mar 16, 2015 07:40 am
Steven DAprano Mar 16, 2015 11:59 am
Marko Rauhamaa Mar 15, 2015 12:48 am
Chris Angelico Mar 15, 2015 02:02 am
Terry Reedy Mar 12, 2015 08:11 pm
Mark Lawrence Mar 17, 2015 03:55 am
Albert van der Horst Mar 31, 2015 12:57 pm
Albert van der Horst Mar 31, 2015 01:18 pm
Dave Angel Mar 31, 2015 01:38 pm
Steven DAprano Apr 03, 2015 06:02 am
Paul Rubin Apr 03, 2015 06:46 am
Albert van der Horst Mar 31, 2015 03:03 pm
Chris Angelico Mar 31, 2015 03:36 pm
Rustom Mody Mar 17, 2015 03:33 am
Mark Lawrence Mar 17, 2015 03:25 am
Marko Rauhamaa Mar 12, 2015 08:22 pm
Messages in this thread