And this learning curve is quite steep.
It is made even harder by the following:
>>> def f(): yield 1
<function f at 0x7f748507c5f0>
So the user is now convinced that 'f' really is a function.
Further investigation makes it even more confusing:
<generator object f at 0x7f74850716e0>
At this point, the user starts to suspect that something is kind of unusual about 'yield' keyword.
Eventually, after a while, the user starts to figure this out:
>>> def f(): print('f started'); yield 1
<generator object f at 0x7f3a0baf5fc0>
And eventually after reading https://docs.python.org/3/reference/datamodel.html the following sentence:
"The following flag bits are defined for
0x04 is set if
the function uses the
*arguments syntax to accept an arbitrary number of
positional arguments; bit
0x08 is set if the function uses the
**keywords syntax to accept arbitrary keyword arguments; bit
0x20 is set
if the function is a generator."
Finally figures it out:
>>> def f(): yield 1
<function f at 0x7f73f38089d8>
>>> f.__code__.co_flags & 0x20
My point is ... this learning process is overly confusing to a new user & not a smooth learning curve.
Here is, just a quick initial proposal on how to fix it:>>> def f(): yield 1
@generator(catpure_context_at_start = true)
def f(): yield 1
With three options: (a) capture context at start; (b) capture context on first call to __next__; (c) don't capture context at all, but have it work the natural way, you expressed two emails ago (i.e.: each time use the context of the caller, not a special context for the generator).
Finally if a user attempts to user a generator with contexts but without one of these three parameters, throw a syntax error & tell the user the context usage must be specified.
The major point of all this, is make the learning curve easier for users, so generators are:
On 15 October 2017 at 13:51, Amit Green <firstname.lastname@example.org> wrote:
> Once again, I think Paul Moore gets to the heart of the issue.
> Generators are simply confusing & async even more so.
> Per my earlier email, the fact that generators look like functions, but are
> not functions, is at the root of the confusion.
I don't agree. I don't find generators *at all* confusing. They are a
very natural way of expressing things, as has been proven by how
popular they are in the Python community.
I don't *personally* understand async, but I'm currently willing to
reserve judgement until I've been in a situation where it would be
useful, and therefore needed to learn it.