I DIDN'T KNOW You Could Do THIS With 'Yield' In Python

preview_player
Показать описание
I didn't know that you could do this with yield in Python. In this video I will be showing you some extra functionality that you can use with the yield keyword when working with generators.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

I remember seeing this when I was learning about the yield keyword, but never actually used it.
When you mention you can use multiple statements in a function at 3:32, it makes sense if you think of the yield keyword as a pause where you return a value, but when calling again (with the next) you resume from that statement. You get a StopIteration when the generator reaches the end of that function, which means no more items

ProfRoxas
Автор

Thanks for educating me. I rarely use yield and so this is even more fascinating for me

ExDarkx
Автор

yield is also an assignment statement:

value = yield value

coroutines.

DrDeuteron
Автор

When one video is Enough.... Good explanation. Thanks.

vaha
Автор

Your python videos are the best on the internet. Thanks so much!

dcollett
Автор

I didn't know you couldn't pickle generators :(. Thought I was being clever. I discovered that you can wrap a generator with enumerate however to easily create an offset that you can save to use if you need to pick up where you left off though you still need to iterate to that point from the beginning. I was hoping a pickle would save the state.

gardnmi
Автор

def random_nums():
for i in range(3):
yield i

def randoms():
for i in range(100, 110):
yield i

def generator():
yield from random_nums()
print("Next")
yield from randoms()

if __name__ == "__main__":
gen = generator()
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))

kvelez
Автор

This is pretty useful for games and for generating preemptively prime numbers and such, in particular because the big work is eventually done in the background (?). I will test that.

yksw
Автор

Thanks for the analysis! 🔍 Just a small off-topic question: 😅 I found these words 😅. (behave today finger ski upon boy assault summer exhaust beauty stereo over). How do I use this? 🤨

DeybRuggs
Автор

Nice explantion.
And about the IDE, is it PyCharm or Fleet?

prasangsinghal
Автор

I really appreciate your efforts! I need some advice: My OKX wallet holds some USDT, and I have the seed phrase. (air carpet target dish off jeans toilet sweet piano spoil fruit essay). Could you explain how to move them to Binance?

WilliansMatinez
Автор

This is not the main purpose of this `yield from <generator>` syntax. See the mcoding video for details

airatvaliullin
Автор

Great video!
I have seen the "yield from" being used somewhere but never dug any deeper into how it works. 👍🏽👍🏽

HeyZReD
Автор

Bravo 🌞 Lit 🌟 Gratitude 🎉 for your satisfactory Work 🚀🌱

CultureofSpeech
Автор

what ide you are using? it looks like vscode but the buttons or run and debug are different

maciejdudek
Автор

Generator comprehantion ist quite useful especially if applying expensive functions 😁

maximpapusha
Автор

Is your IDE VSCode ? It look different

viethungoan
Автор

Explain the last part clearly please what is the need for next keyword there even without next keyword that generator can yield random numbers function but why it execute random number 100() function as a list what is the use of next here why the first random number function didn't execute after removing next (generator)

Riyaalita
Автор

Just like using list comprehensions instead of simple for loops, I like how u can just make only one line instead of several

Cpt.C
Автор

So does that mean you could yield from range(10)??? 🤯

allo