5 Useful Generator Functions In Python

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

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

  • @thomaseb97
    @thomaseb97 5 месяцев назад +12

    if you don't finish the read or csv_row_reader generators, that file is open for the entire duration of the program
    the with block should be outside the function

  • @farzadmf
    @farzadmf 5 месяцев назад +2

    SUPER useful video, thank you!

  • @liamrwilkins
    @liamrwilkins 5 месяцев назад +4

    I've never understood generators in JavaScript, (or any language), but this explained it so well!
    I have so many ideas for where to use these in my projects! (I love that learning about one language can teach you about others as well 😊)

  • @Beauchant
    @Beauchant 5 месяцев назад +10

    This is high level, thank you.

    • @felix30ua
      @felix30ua 5 месяцев назад

      especially, if you don't read books, coal - Fluent Python @ Luciano Ramalho )))))

  • @bincharinnovik1218
    @bincharinnovik1218 2 месяца назад

    This is great explanation and best for me to start learning Python, Thank you very much for demo.

  • @nileshgite1059
    @nileshgite1059 12 дней назад

    Amazing insight ! many thanks

    • @Indently
      @Indently  12 дней назад

      Glad it was helpful!

  • @oli111222
    @oli111222 3 месяца назад +1

    Really nice explanation! Very understandable. Maybe it would help even more if you showe the result first, then went into "how to code it"

  • @akcelero
    @akcelero 5 месяцев назад +3

    hey hey, great material, keep that up!
    just wanted to share with you guys other useful thing about generators:
    `yield from`
    unfortunately I can't past link here to docs
    but anyway - it would simplify iterations in examples : )

    • @digitaldetective47
      @digitaldetective47 5 месяцев назад

      another thing about generators: typing.Generator is deprecated since 3.9; use collections.abc.Generator instead

  • @Slangs
    @Slangs 5 месяцев назад +3

    Can we get a sequel for Bob's story? I am intrigued, it sounds wise

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

    3:04 `i` could be replaced with `_` since the value of `i` is never used (line 18):
    for _ in range(n):

  • @murphygreen8484
    @murphygreen8484 5 месяцев назад

    So good! Very helpful!

  • @briantaylor9958
    @briantaylor9958 2 месяца назад

    Great!

  • @hamidraza1584
    @hamidraza1584 5 месяцев назад

    Great video , love from Lahore Pakistan

  • @EUJokerBR
    @EUJokerBR 5 месяцев назад +2

    This was helpful. But creating generators using classes is way harder I think, you have to set some methods and sometimes the output is the memory address instead of the value you want. Pain

  • @IdPreferNot1
    @IdPreferNot1 5 месяцев назад

    New learning..thx

  • @AlexandrShadchin
    @AlexandrShadchin 5 месяцев назад +1

    typing.Generator is deprecated alias to collections.abc.Generator.

  • @olivierbouchez9150
    @olivierbouchez9150 5 месяцев назад

    is there a difference for exemple 4. if we do yield from sequence, instead the for item in sequence: yield item

    • @felix30ua
      @felix30ua 5 месяцев назад

      these are completely different things, dude - the "yield from" construct delegates the flow of execution to another generator
      use cycle from itertools instead for example 4)))

  • @samuelec
    @samuelec 5 месяцев назад

    Try to keep more code in the viewport so not just the function but also what called it

  • @rishiraj2548
    @rishiraj2548 5 месяцев назад

    Thanks

  • @syrupthesaiyanturtle
    @syrupthesaiyanturtle 5 месяцев назад +1

    what's the difference between a generator, an iterator, and an iterable?

    • @olivierbouchez9150
      @olivierbouchez9150 5 месяцев назад +2

      A generator is a function that are using yield to send the vale back. iterators are objects where the method __iter__() and __next__() are define. A generator compute the value each time next is call. iterators object can store the values and then deliver the value. A list is an iterator. if you repeat a for i in a iterator, you will go through all the element of the list each time starting from the first. if you do a for i in a generator and you have reach the end, you cannot restart from the first one. reading from a file is typically a kind of generator, you can read each line till then end, but you can’t restart from the begining unless you close and reopen the file but technically is creating a new generator. With an iterator, you will load the content of the file in a list with the readlines() method, then iterate again on the same list.

  • @largewallofbeans9812
    @largewallofbeans9812 5 месяцев назад +1

    So… the line reading one is just map(str.strip,open(…))?

    • @minciNashu
      @minciNashu 2 месяца назад

      That's eager loading.

  • @soycrates
    @soycrates 3 месяца назад

    pretty sure iterating over the lines of a file object ensures only one line is read into memory at a time, so not sure the reader generator is actually necessary.

    • @minciNashu
      @minciNashu 2 месяца назад +1

      The examples are too abstract. The line generator is neat when you need the context manager for the file to remain encapsulated in the generator's state, but you also need to advance the file stream and pass it around. For example if you're parsing a file with a specific structure.

  • @ahmoin
    @ahmoin 5 месяцев назад

    8:10 lol numpy calls cumulative sum that too

  • @楊冠曾
    @楊冠曾 5 месяцев назад

    我的QUBIT 怎麼都是上旋?

  • @victorgutierrez6437
    @victorgutierrez6437 2 дня назад

    Why you use -> ??
    I can’t understand 😅

  • @syrupthesaiyanturtle
    @syrupthesaiyanturtle 5 месяцев назад +1

    so yield can send out and receive values?? maybe it should be named into something else...

  • @cloudirector
    @cloudirector 5 месяцев назад

    👽

  • @AkiitaSharpe-d4s
    @AkiitaSharpe-d4s 3 месяца назад

    New to this and I don’t understand 😢

  • @noli-timere-crede-tantum
    @noli-timere-crede-tantum 5 месяцев назад

    How is the fibonacci function useful? Who actually uses it except for someone interviewing a junior dev or someone developing a scrum board app (in which case you only need the first 7 values)

  • @davidmoreno2827
    @davidmoreno2827 5 месяцев назад

    I wanna marry Bob! Thanks

  • @mpty2022
    @mpty2022 5 месяцев назад +7

    thats opposite of what python was developed for