[Python-Dev] code blocks using 'for' loops and generators

Brian Sabbey sabbey at u.washington.edu
Sat Mar 12 21:36:10 CET 2005

On Fri, 11 Mar 2005, Josiah Carlson wrote:

> My first reaction to the proposal was "ick".  Why?  Because every time
> I've seen a mechanism for modifying the internals of generators
> constructed using yield, the syntax has been awful; in my opinion
> (whether my opinion means anything is another matter), the syntax you
> propose is quite awful,

I find it quite natural.  Stuff on the right of 'yield' is going out, 
stuff on the left is coming in.  Since 'yield' is different than return in 
that it marks a spot at which execution both leaves and re-enters the 
frame, it makes sense that 'yield' should have a syntax that indicates as 

> and the functionality you desire does not
> require syntax modification to be possible.

Was the functionality provided by generators or decorators or anything 
else impossible before those were introduced?  Of course not.  The point 
is to make things easier and more natural, not to enable some previously 
impossible functionality.

> Strawman 1: the syntax
> def pickled_file(name):
>     f = open(name, 'r')
>     l yield pickle.load(f)
>     ^
> ------
> |    f.close()
> |    f = open(name, 'w')
> |    pickle.dump(l, f)
> |    f.close()
> |
> While this is currently a syntax error, it is not clear that an
> assignment is happening /at all/, and I don't believe it would be /even
> if/ if were documented, and every time someone opened up Python it said
> "This is an assignment!" with an example. It looks too magical to me
> (and from a guy who had previously proposed 'Some' as the opposite of
> 'None', that's saying something).

Perhaps you are right, I don't know.  It seems to me that most people 
would have to see the syntax once to know exactly what is going on, but I 
certainly don't know that for sure.  Either way, I'd hate to have all my 
suggestions dismissed because of the syntax of this one piece.

> Strawman 2: putting data back into a generator
> def pickled_file(name):
>     f = open(name, 'r')
>     yield pickle.load(f)
>     f.close()
>     f = open(name, 'w')
>     pickle.dump(l, f)
>     f.close()
> Keep your code, except toss that leading 'l'.
> for l in pickled_file('greetings.pickle'):
>     l.append('hello')
>     l.append('howdy')
> Toss that 'continue l', and your code will work (as long as both the
> function and the for are sitting in the same namespace).

But they're *not* in the same namespace necessarily.  That is entirely the 
point.  One is changing scope but has no clean way to pass values.  How is 
making 'l' some (more) global variable possibly a clearer way to pass it 
to the generator?  Your argument is like saying one does not need to 
return values from a function because we could always just use a global 
variable to do it.

> Hrm, not good enough?  Use a Queue, or use another variable in a
> namespace accessable to both your function and your loop.

Again, I entirely realize it's possible to do these things now, but that 
is not the point.

> I'm sorry if this email comes off harsh,

I'm just relieved someone responded :)


More information about the Python-Dev mailing list