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
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))
I dislike: def meth() result = [] for.... result.append(something) return result It's clumsy, so I'll do def imeth() for.... yield something def meth(): return list(self.imeth()) I find it easier to debug, and look at.
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.
Why would you need to iterate? In many generators, specially built-in functions, you can start at a given iteration, enumerate can have a start value and you can split the list or other iterator with values you don't need and only keep the ones you do need
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.
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?
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? 🤨
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)
Pretty basic, honestly I was expecting to learn about the true reason behind why the yield from statement was added to python, not this kind of shortcut usage
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
yield is also an assignment statement:
value = yield value
coroutines.
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))
Thanks for educating me. I rarely use yield and so this is even more fascinating for me
I dislike:
def meth()
result = []
for....
result.append(something)
return result
It's clumsy, so I'll do
def imeth()
for....
yield something
def meth():
return list(self.imeth())
I find it easier to debug, and look at.
Very simple explanation, thank you
I love the way you teach. 😀
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.
Why would you need to iterate? In many generators, specially built-in functions, you can start at a given iteration, enumerate can have a start value and you can split the list or other iterator with values you don't need and only keep the ones you do need
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.
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?
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? 🤨
Pretty slick. Great explanation
When one video is Enough.... Good explanation. Thanks.
Your python videos are the best on the internet. Thanks so much!
This is not the main purpose of this `yield from ` syntax. See the mcoding video for details
Bravo 🌞 Lit 🌟 Gratitude 🎉 for your satisfactory Work 🚀🌱
what ide you are using? it looks like vscode but the buttons or run and debug are different
It's PyCharm, made by JetBrains
Nice explantion.
And about the IDE, is it PyCharm or Fleet?
It's PyCharm
Great video!
I have seen the "yield from" being used somewhere but never dug any deeper into how it works. 👍🏽👍🏽
Generator comprehantion ist quite useful especially if applying expensive functions 😁
Sehr gut!!
Very informative
thanks. learned it new
Nice, thanks for sharing
Just like using list comprehensions instead of simple for loops, I like how u can just make only one line instead of several
If you try to use yield from in async function, you'll see a nice error.
Great content. Subbed!
Is your IDE VSCode ? It look different
pycharm
Thanks for knowledge
Thanks, and notice the numbers aren't random 😅
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)
next just takes 1 item out of the iterator. list takes them all.
This is cool!
So does that mean you could yield from range(10)??? 🤯
Correct
Thanks a million
what is that IDE?
Looks like Visual Studio Code
Pycharm
I had a use of this in streaming live vedio in opencv flask setup
👍
3rd
Pretty basic, honestly I was expecting to learn about the true reason behind why the yield from statement was added to python, not this kind of shortcut usage
No ego only education 🫂