In this video, Josh McQuiston explains how to construct a coroutine decorator. Learn how this decorator can be used to avoid easy mistakes and provide for clean, modular code. The coroutine decorator enhances coroutine functions to produce pre-primed objects by using the python decorator syntax, thus avoiding unecessary boilerplate code.
- [Instructor] I've talked about the need…to prime a coroutine by calling next on it…or by sending a None value to it.…I'll show you why that has to be done…and an easy way to get around this.…If we take a look at the counter function…from the last video, this will start from the top…and execute until it reaches the first yield.…Until it has paused at yield,…sending a value to the coroutine simply won't work.…I'll open the shell and send a value to it…before I have primed it so we can see the error again.…
This error is pretty descriptive of what I did wrong.…It let's me know that I must either call next…or send a None value so that the coroutine object…has advanced to the point of being suspended…at the yield point.…Having to prime a coroutine seems a little hackey at best…and it's easy to forget.…An easy solution to this problem is a coroutine_decorator.…I'll show you what this looks like and how it works.…I've opened up O3_O3\coroutine_decorator.…
Coroutine_decorator is a function…that is to be used as a decorator function.…
Share this video
Embed this video
Video: Build a @coroutine decorator