Calling a generator multiple times

Courageous jkraska at san.rr.com
Sun Dec 9 12:21:45 EST 2001


>Nobody (rational) expects that.  A BDFL Pronouncement is called for
>precisely in those areas where extended debate has proved that consensus
>will never be reached.  The choices then are eternal paralysis, or, if Guido
>wants something enough, he breaks the deadlock by Decree and life moves on.

This is fair enough, and I can live with it. It's not like generators
are all that hard, or anything. And it's not that I think that the
current _behavior_ of generators is wrong; rather, I'm flummoxed as
to the leaving of the syntax as it is, as it leads to surprises.
The syntax as is predicts other behavior to me.

For example, when I wrote my first generator, I knew _conceptually_
exactly what to expect based on my prior use of stackless
continuations, continuations in lisp, cooperative multi-threading
environments I've written, and esoteric and ill-advised :) use of
setjmp and longjmp in the past.

However, what threw me was the behavior on the first invocation.
Since I was defining a method, I expected that when called the
first time the code prior to first yield would execute. That's
not what happened. The code executed on the first next.

I can see perfectly well why it went the other way, but I certainly
hope you can see how I would state that code appearing in a function
definition probably ought to execute when the function is apparently
called or someone is going to be surprised.

And they will be. I guarantee it. I'm willing to bet that almost
every single new user of generators is going to encountered this
and have the same experience of surprise.

Will this _baffle_ anyone? Hell, no. It's not that hard to say
"what the fuck!?", read the manual a little more carefully, and
come to an understanding. It's not a _difficult_ concept to
grasp, just an unexpected one.

C//




More information about the Python-list mailing list