Yield didn't return anything in the previous examples.
It is possible to catch the yielded value in a variable in the as clause:
from contextlib import contextmanager
@contextmanager
def tag(name):
print("<%s>" % name)
yield 42
print("</%s>" % name)
with tag("h1") as r:
print("My string and the return value: ", r)
The output:
<h1>
My string and the return value: 42
</h1>