| Store | Cart

Re: generator/coroutine terminology

From: Steven DAprano <stev...@pearwood.info>
Mon, 16 Mar 2015 22:51:21 +1100
Chris Angelico wrote:

> On Mon, Mar 16, 2015 at 7:36 PM, Steven D'Aprano> <stev...@pearwood.info> wrote:>> 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.>>> > Hmm. That would imply that an expression is different if it's used> somewhere. In Python, there's no difference between these two function> calls:> > x = func(1,2,3)> func(1,2,3)> > except for the actual name binding.

That's a pretty big difference though :-)

Just for the record, it's not just name bindings that make a generator
behave as a coroutine. E.g.:

py> def co():
...     print( (yield 1) + 2 )
...
py> a = co()
py> next(a)
1
py> a.send(98)
100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration


Not the most *useful* example, I grant, but it demonstrates the idea.
However, for simplicity let's ignore that and pretend it's only name
binding we care about.


> If a yield expression enabled > send() if and only if its return value were used, then it would be> extremely surprising:

It's surprising to me that you can send() into a non-coroutine and have it
ignored. I'm not even sure that is documented behaviour or just an accident
of implementation.

 
> def use_and_discard():>     while True:>         _ = (yield 1)> > def just_yield():>     while True:>         yield 1> > So... I can send into the first but not into the second?

That's the idea.

The second one is not a coroutine, and cannot be used as a coroutine in any
useful way. I maintain that it is a mistake that you can send() into it at
all, and if somebody does, it is invariably an error. That would be like
send()ing into None, or a str. We know what the Zen says about errors...


> That said, though, it would be quite reasonable for a *linter* to warn> you about sending into a generator that never uses sent values. With a> good type inference system (not sure if MyPy is sufficient here), it> would be possible to distinguish between those two functions and> declare that the first one has the return type "sendable_generator"> and the second one "nonsendable_generator", and give a warning if you> send into the latter. But I don't think that's the language's job.

I do :-)

I think it would be hard for a linter to do this. It can't just do a
type-check, because the types of generator-iterators and
generator-coroutines are the same. It would need to actually analyse the
source code, AST, or byte-code. That's doable, but the compiler already
does that, and compiles different code for the two cases:

py> from dis import dis
py> def g1():
...     yield 1
...
py> def g2():
...     x = yield 1
...
py> dis(g1)
  2           0 LOAD_CONST               1 (1)
              3 YIELD_VALUE
              4 POP_TOP
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE
py> dis(g2)
  2           0 LOAD_CONST               1 (1)
              3 YIELD_VALUE
              4 STORE_FAST               0 (x)
              7 LOAD_CONST               0 (None)
             10 RETURN_VALUE


so it should be easy for the compiler to recognise a yield used as if it
were a statement versus one used as an expression, and take appropriate
steps. But maybe there are subtle corner cases I haven't thought of.



-- 
Steven

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