Why can't a yield expression be a function argument?

Viewed 438

I simply want to tidy up the following code.

def well_known_generator():
    print('started')
    received = yield 100
    print(received)
    received = yield 200
    print(received)
    received = yield 300
    print('end')

g = well_known_generator()
print(next(g), g.send(None), g.send(None), g.send(None))

I just moved the yield expression into the print function, but a syntax error occurs. I'm just wondering why a yield expression can't be a function argument like below? If yield works like an expression, it should be okay as a function argument.

def well_known_generator():
    print('start')
    print(yield 100)
    print(yield 200)
    print(yield 300)
    print('end')

g = well_known_generator()
print(next(g), g.send(None), g.send(None), g.send(None))

SyntaxError: invalid syntax (<ipython-input-58-bdb3007bb80f>, line 3) 
  File "<ipython-input-58-bdb3007bb80f>", line 3
    print(yield 100)
              ^
SyntaxError: invalid syntax
1 Answers
Related