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

Поделиться
HTML-код
  • Опубликовано: 24 окт 2024

Комментарии • 48

  • @ProfRoxas
    @ProfRoxas Год назад +4

    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

  • @DrDeuteron
    @DrDeuteron Год назад +7

    yield is also an assignment statement:
    value = yield value
    coroutines.

  • @kvelez
    @kvelez 11 месяцев назад +5

    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))

  • @ExDarkx3
    @ExDarkx3 Год назад +9

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

    • @DrDeuteron
      @DrDeuteron Год назад

      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.

  • @sultanahmed9694
    @sultanahmed9694 8 месяцев назад

    Very simple explanation, thank you

  • @Greenbay-bn3yk
    @Greenbay-bn3yk 3 месяца назад

    I love the way you teach. 😀

  • @gardnmi
    @gardnmi Год назад +8

    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.

    • @alenasenie6928
      @alenasenie6928 Год назад +1

      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

  • @y2ksw1
    @y2ksw1 6 месяцев назад

    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.

  • @WilliansMatinez
    @WilliansMatinez Месяц назад

    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?

  • @DeybRuggs
    @DeybRuggs Месяц назад

    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? 🤨

  • @comfixit
    @comfixit 9 месяцев назад

    Pretty slick. Great explanation

  • @vaha2833
    @vaha2833 Год назад

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

  • @dcollett
    @dcollett Год назад

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

  • @airatvaliullin8420
    @airatvaliullin8420 Год назад +1

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

  • @CultureofSpeech
    @CultureofSpeech 6 месяцев назад

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

  • @maciejdudek2381
    @maciejdudek2381 6 месяцев назад

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

    • @banana1093
      @banana1093 6 месяцев назад

      It's PyCharm, made by JetBrains

  • @prasangsinghal262
    @prasangsinghal262 8 месяцев назад

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

  • @HeyZReD
    @HeyZReD Год назад +2

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

  • @maximpapusha5032
    @maximpapusha5032 Год назад

    Generator comprehantion ist quite useful especially if applying expensive functions 😁

  • @rondamon4408
    @rondamon4408 7 месяцев назад +1

    Sehr gut!!

  • @nextisnowmain
    @nextisnowmain 6 месяцев назад

    Very informative

  • @rryann088
    @rryann088 Месяц назад

    thanks. learned it new

  • @castlecodersltd
    @castlecodersltd Год назад +1

    Nice, thanks for sharing

  • @Cpt.C
    @Cpt.C Год назад

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

  • @aaliboyev
    @aaliboyev 5 дней назад

    If you try to use yield from in async function, you'll see a nice error.

  • @redpillsatori3020
    @redpillsatori3020 Год назад

    Great content. Subbed!

  • @viethungoan7275
    @viethungoan7275 Год назад

    Is your IDE VSCode ? It look different

  • @alias77799
    @alias77799 6 месяцев назад

    Thanks for knowledge

  • @seanshimon
    @seanshimon 3 дня назад

    Thanks, and notice the numbers aren't random 😅

  • @Riyaalita
    @Riyaalita Год назад

    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)

    • @DrDeuteron
      @DrDeuteron Год назад

      next just takes 1 item out of the iterator. list takes them all.

  • @ricgondo
    @ricgondo Год назад

    This is cool!

  • @allo5668
    @allo5668 Год назад +1

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

  • @rishiraj2548
    @rishiraj2548 Год назад

    Thanks a million

  • @ballex2534
    @ballex2534 Год назад

    what is that IDE?

  • @jasonbourn29
    @jasonbourn29 Год назад

    I had a use of this in streaming live vedio in opencv flask setup

  • @sg8581
    @sg8581 Год назад

    👍

  • @betapacket
    @betapacket Год назад

    3rd

  • @joaquinfernandez6066
    @joaquinfernandez6066 6 месяцев назад +1

    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

  • @sprayg0d759
    @sprayg0d759 6 месяцев назад

    No ego only education 🫂