Go Straight to Content

What I'll Be Ranting About

Good development practices bring us quality code, confident systems, and missed launch windows. When do you refactor and when do you factor in the passing time? As engineers we need to design what is possible and capable. As programmers we need to turn imagination into reality without a physical product. As developers we need to bridge the gab between that engineered vision and the end product.

I also blog more personally over at my tumblr page.

I am available for small contracts, consultations, tutoring, and other development services. My "skills" as a technical writer are also available. If you've got anything you'd like to talk to me about or for me to see, drop me a line.

Monday, April 16, 2007

Notes on generators, yield expressions, and order of execution

I was messing around with the use of yield as an expression, the new feature in Python 2.5, and I got a little tripped in small cases of order with how you need to iterate over the generator and when you call send(), etc. I just thought I would post this example to make anyone passively reading aware of the ordering.


>>> def g():
... print 1
... print 2, (yield)
... print 3
...
>>> gen = g()
>>> gen.next()
1
2
>>> gen.send(0)
0
3
Traceback (most recent call last):
File "", line 1, in
StopIteration
>>>

2 comments:

Jack said...

Each of the print arguments is evaluated left-to-right just before they are needed. If you think of the '(yield)' as '(yield(None))' it might read easier [NB: those extra parens are just grouping and not a function call!]

Here's a similar situation using functions instead:


>>> def a():
... print "A",
... return "ret_A"
...
>>> def b():
... print "B",
... return "ret_B"
...
>>> print 1, a(), 2, b(), 3
1 A ret_A 2 B ret_B 3

Anonymous said...

WOW, so when they said generators halt evaluation at yield, they REALLY mean it!

Blog Archive