Generator - What happens when a Python yield statement has no expression

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m a C# programmer trying to understand some Python code. The code in question is a generator function, and looks like this:

def func():
    oldValue = curValue
    yield
    curValue = oldValue

If I understand this correctly, this will generate a iterable sequence with one member. However, there is no expression after the yield statement. What is such an expression-less statement supposed to yield? Are there any Python idioms that make use of this way of coding?

Answers

It ll yield None; just like an empty return expression would:

>>> def func():
...     yield
... 
>>> f = func()
>>> next(f) is None
True

You d use it to pause code . Everything before the yield is run when you first call next() on the generator, everything after the yield is only run when you call next() on it again:

>>> def func():
...     print("Run this first for a while")
...     yield
...     print("Run this last, but only when we want it to")
... 
>>> f = func()
>>> next(f, None)
Run this first for a while
>>> next(f, None)
Run this last, but only when we want it to

I used the two-argument form of next() to ignore the StopIteration exception thrown. The above doesn t care what is yielded, only that the function is paused at that point.

https://docs.python.org/2/library/contextlib.html#contextlib.contextmanager https://docs.python.org/2/library/contextlib.html#contextlib.contextmanager

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/22960397/what-happens-when-a-python-yield-statement-has-no-expression

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils