Decorates yield function. Last time I’ve covered the basic of python’s decorator. Normally python’s decorator intercepts the value that runs into, and out of your function. But what if, the result is returned as Generator instead?

Decorates yield function

So the idea, is simple, since Python’s Generator generates result. (The one that we suppose to decorate). We can have our decorator function call generator.next() per yield requested. The idea would be more simpler to explain:

As you can see yield_decorator enforce generator.next() through use of for-loop. And then within that loop, we decorate the result. Then simply yield it back.

Using it

Here some example for better understanding what it is.

Here is the result: