You are not indexing. You get a list; expression yield[0] really the same as the following (but without a variable):
lst = [0] yield lst
If you look what next() returns, you would get this list:
>>> def gen1(): ... t = yield[0] ... assert t ... yield False ... >>> g = gen1() >>> next(g) [0]
You do not need to have a space between yield and [0] , that's all.
The exception is caused by the fact that you are trying to apply a subscription to the contained 0 integer:
>>> [0] # list with one element, the int value 0 [0] >>> [0][0] # indexing the first element, so 0 0 >>> [0][0][0] # trying to index the 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not subscriptable
If you want to index the value sent to the generator, put the brackets around the yield expression:
t = (yield)[0]
Demo:
>>> def gen1(): ... t = (yield)[0] ... print 'Received: {!r}'.format(t) ... yield False ... >>> g = gen1() >>> next(g) >>> g.send('foo') Received: 'f' False
source share