Python Generators 2: send and yield

  Рет қаралды 8,753

Sebastiaan Mathôt

Sebastiaan Mathôt

Күн бұрын

Пікірлер: 22
@andrewstebbing1646
@andrewstebbing1646 5 жыл бұрын
This is an excellent summary of Python generators. I'm fairly new to Python and have seen and read a bit about generators, but this is the first time anyone has described bi-directional communication using send(). This makes them far more powerful than I realised. Thank you for such an informative explanation.
@kingsgambit
@kingsgambit 3 жыл бұрын
These videos are some of the best Python tutorials on KZbin. What makes them so good is how precise, short, compact, yet detailled you explain the matter and support it with well chosen examples. Perfect!
@pepijn1287
@pepijn1287 5 жыл бұрын
The clearest explanation I've seen! Thanks!
@tymofiidmytrenko3418
@tymofiidmytrenko3418 4 жыл бұрын
Awesome explanation of .send method of generators. I tried to grasp the concept for a while, but with no luck... this video just rocks and I love the game based demo as well! I have now subscribed to your channel.
@Zakaramut
@Zakaramut 6 жыл бұрын
great teaching man, love your work!
@jonahnio5642
@jonahnio5642 5 жыл бұрын
Hi Sebastian thanks so much for the video which will help me apply the asynchronous concept to my work. I also watched your video on asyncio and not sure what to use .what is the advantage of using asyncio over generators thanks a lot
5 жыл бұрын
Think of asyncio as a framework that is based on generator functions, which are often called coroutines in this context. Whether you need all the superpowers of asyncio, or whether regular generators are sufficient depends on what you want to do!
@johnnybravo2342
@johnnybravo2342 4 жыл бұрын
To anyone watching: In some version of python behavior of generators changed and with code presented in a video it returns with both StopIteration and RuntimeError exception. According to github.com/allenai/bilm-tf/issues/170#issuecomment-469713724 you have to change 'raise StopIteration' to simply 'return' and the exception will be handled as it should. Hope i helped :)
@maicoldlb
@maicoldlb 5 жыл бұрын
Thanks a lot for this !!
@caseyalanjones
@caseyalanjones 4 жыл бұрын
This helped me a lot, but what I still don't get is the return value of the yield statement for the first iteration... If you send None to icat for the first iteration, then wouldn't mouse then be None in "mouse = yield cat"? I suppose not, since you otherwise wouldn't have been able to access col and row attributes on a NoneType object... but I don't get how this works. How is mouse an Animal on the first iteration, even though you send None? That's the only part I'm still confused about. Otherwise fantastic explanation and demo! Thanks a lot.
@caseyalanjones
@caseyalanjones 4 жыл бұрын
Never mind, I think I figured it out. In your example a value is yielded, execution of the generator is paused, then a value is sent in - so the None value is already overwritten as soon as execution returns to the generator and before Animal attributes are accessed.
@billybabcokcs8224
@billybabcokcs8224 5 жыл бұрын
great work
@nairazak_art
@nairazak_art 2 жыл бұрын
Thank you, I was reading Effective Python and I wasn't able to understand bidirectional communication
@RafaganAbreu
@RafaganAbreu 4 жыл бұрын
Very nice tutorial!
@gilbertodelavega359
@gilbertodelavega359 3 жыл бұрын
I don't understand why you assigned the value of the yielded value to the mouse in the line "mouse = yield cat"? Wouldn't that overwrite the value of mouse that was passed in and make mouse and cat have the same value? I don't understand that line of code at all...
@JoaoVitorBRgomes
@JoaoVitorBRgomes 3 жыл бұрын
No. mouse = yield cat is not the same as mouse = cat.
@caseyalanjones
@caseyalanjones 4 жыл бұрын
WorkRave cameo 😂
@JeaneAdix
@JeaneAdix 6 жыл бұрын
are you norwegian?
6 жыл бұрын
Close, I'm Dutch!
@MrDmitriM
@MrDmitriM 5 жыл бұрын
+++
@Kornelux95
@Kornelux95 5 жыл бұрын
4:25 StopIteration exception is not a python's way to tell us generator functions is exhausted. StopIteration exception is a python's way to tell that Iterator's stream of data has ended (when there is no more data to return/yield). Generator functions returns object called Generator Iterator. So to be more precise when we get StopIteration in 4:25 it means that the Generator Iterator's stream of data is exhausted.
@greob
@greob 6 жыл бұрын
Very nice!
Python Generators 1: Functions that yield, suspend, and resume
10:54
Sebastiaan Mathôt
Рет қаралды 11 М.
Application logging in Python | Python tricks
18:43
Sebastiaan Mathôt
Рет қаралды 11 М.
Real Man relocate to Remote Controlled Car 👨🏻➡️🚙🕹️ #builderc
00:24
Profiling and optimizing your Python code | Python tricks
17:11
Sebastiaan Mathôt
Рет қаралды 71 М.
Python Decorators 1: The Basics
11:22
Sebastiaan Mathôt
Рет қаралды 40 М.
Python tricks: All there is to know about Exceptions
18:12
Sebastiaan Mathôt
Рет қаралды 18 М.
Python Generators
15:32
mCoding
Рет қаралды 140 М.
Python tricks: Type hints and static type checking
14:15
Sebastiaan Mathôt
Рет қаралды 21 М.
WHY IS THE HEAP SO SLOW?
17:53
Core Dumped
Рет қаралды 267 М.
Python tricks: Using magic (dunder) functions to create list-like objects
17:59
Async for loops in Python
16:36
mCoding
Рет қаралды 66 М.
List comprehensions in Python [Python comprehensions #1]
17:47
Sebastiaan Mathôt
Рет қаралды 7 М.
What do tech pioneers think about the AI revolution? - BBC World Service
25:48