Generators in Python

preview_player
Показать описание
Learn how to create generator functions, and how to use the "yield" statement. Generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.

You'll also compare list comprehensions and generator expressions. Once you have learned the difference in syntax, you'll compare the memory footprint of both, and profile their performance using cProfile.

Рекомендации по теме
Комментарии
Автор

Love such micro tutorials because it is easier to understand! Keep going! Thank you!

virgilmcmayers
Автор

Thanks for your works, although I am beginner, i am interested all things python, thank for your hard works, very much appreciated!

LeoTwai
Автор

Well explained, real life examples would be much appreciated.

tareqmonwer
Автор

Nice, Clean, into the point, thanks for this great work

mea
Автор

Just what I needed. Thanks for this channel

lordswaggity
Автор

I know generators but I've never needed them. Or at least didn't know I need them. I'm looking forward if you plan to present some use cases from --real world--.

songokussjcz
Автор

Nice presentation. @9:57, sum([i**2 ...]) instead of sum([i*2 ..]) may take a while, and would have returned 0.024 secs and 0.031 secs. So Its not about twice the time for the generator. Thanks for showing us how the generator with yield work. I thought only one yield is allowed in the loop.

gopsu
Автор

Hey Dan. Wasn't sure where to send this. I am having trouble understanding @property in classes. I usually understand everything else pretty quick so it is odd that this defeats me. Could I suggest a video on this topic? I haven't found one yet that does the trick. Thanks!

kevinfoster
Автор

I see you guys want a real world example:
I used it to allocate first available ID number from a range of 0 to 9999000. Problem is that a user can select a free number anywhere in the range and used ID numbers can be released as time progresses(not needed by the service anymore). To not waste ID's I have to basically loop through every existing ID from start to end and check if +1 of that ID exist. If it doesn't it means I can reserve it. Plain for loop will take too long to do this and will hog memory especially once there are many IDs. Generator does it comfortably under a second (which is OK for my application) even when the pool is just about to be full.

uporabnk
Автор

I have learned so far that things that cost less memory run faster on the computer. Why, in this case, is it the opposite? Or maybe I'm not understanding what memory saving means.

andrencfilho
Автор

Anyone knows what ide is used in this video?

teresacruz
visit shbcf.ru