Тёмный

What exactly is 'yield from' in Python? [Easy explanation] 

Indently
Подписаться 224 тыс.
Просмотров 15 тыс.
50% 1

Опубликовано:

 

28 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 48   
@ridwanray
@ridwanray 2 месяца назад
Good job. Personally, I prefer real use cases combined with building projects. That’s how to solidify the knowledge. That’s exactly what am doing on my new channel.
@Indently
@Indently 2 месяца назад
Smooth self promotion ahah
@attacksoil
@attacksoil Месяц назад
pretty cool, and useful when chaining generator
@huongly3001
@huongly3001 2 месяца назад
I watch your videos for entertainment because I’m bored of tarot pick-a-card ones. Thank you for making educational videos in short form ❤
@cocoatea57
@cocoatea57 2 месяца назад
Thanks Freddie
@davidlu1003
@davidlu1003 2 месяца назад
Thx, very useful and save time. Better than reading a textbook, if you want to save your time, my opinion.😁😁😁
@dipeshsamrawat7957
@dipeshsamrawat7957 2 месяца назад
Thank you 😊
@JimRohn-u8c
@JimRohn-u8c 2 месяца назад
So using “yield” seems to track the state of an Iterable? Is that understanding correct? But when are the main cases we would need to use it? Still not understanding.
@Pearoo
@Pearoo 2 месяца назад
@@JimRohn-u8c basically, yield is more memory efficient than returning a huge list and iterating through that list
@glensmith491
@glensmith491 2 месяца назад
When you are applying your iterable to an expensive function, have a very large iterable and/or need a very low memory low, yield is a good candidate to consider for your solution. I use it often when I do large number of simulations such as Monte Carlos.
@JimRohn-u8c
@JimRohn-u8c 2 месяца назад
@@glensmith491 thank you!
@JimRohn-u8c
@JimRohn-u8c 2 месяца назад
@@Pearoo thank you!
@y2ksw1
@y2ksw1 2 месяца назад
I have seen generators in a card game as a state machine, but I still don't grasp it. Is there a way to restart the generator or skip items programmatically? It just doesn't make sense to me. I rather continue to use a function which returns me a value I expect to get.
@DrDeuteron
@DrDeuteron 2 месяца назад
no, that's good. you shuffle the deck into a collections deque, wrapped in your personal Deck class with a dunder next that calls deque's popleft(). that's nice. You can't go back in a deal. So for hold em you;d have "burn" method which calls next(self), while the deal(self, n, m) (n=players, m=hole cards which is 2 for hold em, 4 for PLO), which would deal n*m into some zip() construction.
@y2ksw1
@y2ksw1 2 месяца назад
@@DrDeuteron The scope is this, but I lack understanding 😅
@DrDeuteron
@DrDeuteron 2 месяца назад
@@y2ksw1 maybe just write some code that uses them? Like a toy card game for instance. Learn by using? Then you 'll see where they work better than containers (list, tuple..). Also: itertools "tee" function is pretty cool, if you have terms in a series that go to two different formula (e.g. sum(x^2) and sum(x)^2 calculations, it's very cool. It makes a difference when the computations are hard and the terms in the series a huge messes, e.g some big data file or something). Actually that's a good case--say you want read 1000 100kB data files and compute a metric from it. Your not going to open 1000 files and load them into a list and then loop over that, rather: a generator that yields each file into a metric computing function, so the user just see "file name" turned into 1000 element array, and dev sees each step separated into. The file read can be an iterator to, so you yield file reading iterators straight into a function of it's contents. idk...if that makes sense at all in a yt comment. Need a white board.
@anon_y_mousse
@anon_y_mousse Месяц назад
Basically it's lazy evaluation as someone else's function. You can get the same behavior in your own code if you write the loop yourself, it's just that sometimes you want to call it at different intervals and the lazy evaluation aspect means you might use less memory and not take an inordinate amount of time. To use an example from C, it's the difference between repeatedly calling strtok() to split a string and having a function that you write which creates an array of strings and splits it all in one go. I'm not as versed in Python and don't know if you can change inputs to a generator on the fly, but in C you can change the separators you pass to strtok() with any given call and it'll work correctly, whereas if you had an all in one function you'd either have to account for an array of multiple separator strings, or only get one set of separators. If that's not clear enough, then it might be worth it to ask @Indently to make a video explaining the implementation details of the generator construct, if he hasn't already, and he may have, I just haven't watched all of his videos.
@y2ksw1
@y2ksw1 Месяц назад
@@anon_y_mousse Ah, this makes much more sense now! Thank you very much for your effort!🤗
@VK-qh6pr
@VK-qh6pr 2 месяца назад
Still not sure at which case to avoid return and prefer yield. Maybe overheard or you can mention it in some short words?
@donaldlee8249
@donaldlee8249 2 месяца назад
Use yield if the result can be computed at need
@tirthankarsarkar4206
@tirthankarsarkar4206 2 месяца назад
​@@donaldlee8249could you perhaps share a use case or a scenario for the same?
@DrDeuteron
@DrDeuteron 2 месяца назад
really? I just don't ever want to initialize and empty list and fill it with a loop (or thing that are two complex for a comprehension, and things that are computationally hard).
@donaldlee8249
@donaldlee8249 2 месяца назад
@@tirthankarsarkar4206 for example if a list is super long and takes up a huge chunk of memory, but you only need to access the elements once at a time, you should use yield, since it’s only take up the memory space of that element
@BoringExtrovert
@BoringExtrovert 2 месяца назад
@@tirthankarsarkar4206let’s say you are looping through file names in a directory. Instead of loading all file names onto memory, you can instead compute and load one at a time. This will greatly reduce your memory overhead. Or simply when reading a file, you don’t load the whole thing onto the memory and process it, you process it chunk by chunk only when you need to do it
@Fireman9143
@Fireman9143 2 месяца назад
Since you used wrapper, does this work as a decorator?
@razathewanderer
@razathewanderer 2 месяца назад
He just named it wrapper , should be restrictive keyword 😂
@pokerchannel6991
@pokerchannel6991 Месяц назад
I am no longer using a lot of typing. I think colletion is the new typing
@TheMathues123
@TheMathues123 Месяц назад
I thought the yield from syntax was deprecated since adding coroutines
@Indently
@Indently Месяц назад
Where did you read about the deprecation?
@TheMathues123
@TheMathues123 Месяц назад
@@Indently youtube videos, not a PEP
@Mr.Derpus
@Mr.Derpus 2 месяца назад
first
@Pearoo
@Pearoo 2 месяца назад
HELLO DADDY INDENTLY thanks for improving my knowledge on python on god
@adrianphilippcabunagan6853
@adrianphilippcabunagan6853 2 месяца назад
HUH?
@cslearn3044
@cslearn3044 2 месяца назад
Pardon?
@Sinke_100
@Sinke_100 23 дня назад
Damn, this anottations are so anoying
@youtubeacc666
@youtubeacc666 2 месяца назад
This is nowhere near an easy explanation... Only the ones who knows this will follow this... Not a good channel to learn python... Watch Corey Schafer instead
@Indently
@Indently 2 месяца назад
Sorry to hear it was tough for you to understand, maybe you could share some constructive feedback and I'll use it to improve future videos :)
@ridwanray
@ridwanray 2 месяца назад
Maybe you get to understand it better when used in a real project.
@youtubeacc666
@youtubeacc666 2 месяца назад
@@Indently honestly it feels like you are not trying to simplify it with the intention of teaching someone but rather intentionally overcomplicating things to show off your skills... I am not a fanboy but check Cory Schafer's video on the same topic and see the difference
@Indently
@Indently 2 месяца назад
I see, thank you for the feedback!
@mujykun
@mujykun 2 месяца назад
​@@Indentlyi honestly think it was a good explanation and not difficult. I think they were just experiencing what people call a skill diff
Далее
“typing” is getting deprecated in Python
7:20
Просмотров 75 тыс.
Learn Python OOP in under 20 Minutes
18:32
Просмотров 83 тыс.
20 Everyday Tips & Tricks in Python
25:18
Просмотров 25 тыс.
10 Nooby Mistakes Devs Often Make In Python
24:31
Просмотров 65 тыс.
Why More People Dont Use Linux
18:51
Просмотров 290 тыс.
15 Python Libraries You Should Know About
14:54
Просмотров 397 тыс.
The Flaws of Inheritance
10:01
Просмотров 961 тыс.
5 Really Cool Python Functions
19:58
Просмотров 64 тыс.
5 Useful Dunder Methods In Python
16:10
Просмотров 62 тыс.
5 Uncommon Python Features I Love
15:09
Просмотров 159 тыс.
Python Generators Explained
28:37
Просмотров 157 тыс.