5 Useful Generator Functions In Python

Поділитися
Вставка
  • Опубліковано 6 лис 2024

КОМЕНТАРІ • 38

  • @thomaseb97
    @thomaseb97 2 місяці тому +7

    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

  • @Beauchant
    @Beauchant 3 місяці тому +10

    This is high level, thank you.

    • @felix30ua
      @felix30ua 2 місяці тому

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

  • @oli111222
    @oli111222 Місяць тому +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"

  • @luketurner314
    @luketurner314 2 місяці тому +9

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

  • @liamrwilkins
    @liamrwilkins 3 місяці тому +3

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

  • @farzadmf
    @farzadmf 3 місяці тому +1

    SUPER useful video, thank you!

  • @akcelero
    @akcelero 3 місяці тому +2

    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 2 місяці тому

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

  • @IdPreferNot1
    @IdPreferNot1 3 місяці тому

    New learning..thx

  • @murphygreen8484
    @murphygreen8484 2 місяці тому

    So good! Very helpful!

  • @Slangs
    @Slangs 2 місяці тому +1

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

  • @rishiraj2548
    @rishiraj2548 3 місяці тому

    Thanks

  • @hamidraza1584
    @hamidraza1584 3 місяці тому

    Great video , love from Lahore Pakistan

  • @olivierbouchez9150
    @olivierbouchez9150 2 місяці тому

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

    • @felix30ua
      @felix30ua 2 місяці тому

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

  • @EUJokerBR
    @EUJokerBR 3 місяці тому +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

  • @samuelec
    @samuelec 2 місяці тому

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

  • @largewallofbeans9812
    @largewallofbeans9812 3 місяці тому +1

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

  • @syrupthesaiyanturtle
    @syrupthesaiyanturtle 2 місяці тому +1

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

    • @olivierbouchez9150
      @olivierbouchez9150 2 місяці тому +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.

  • @AlexandrShadchin
    @AlexandrShadchin 2 місяці тому

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

  • @楊冠曾
    @楊冠曾 3 місяці тому

    我的QUBIT 怎麼都是上旋?

  • @soycrates
    @soycrates 28 днів тому

    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 2 місяці тому

    8:10 lol numpy calls cumulative sum that too

  • @cloudirector
    @cloudirector 3 місяці тому

    👽

  • @syrupthesaiyanturtle
    @syrupthesaiyanturtle 2 місяці тому +1

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

  • @AkiitaSharpe-d4s
    @AkiitaSharpe-d4s 25 днів тому

    New to this and I don’t understand 😢

  • @davidmoreno2827
    @davidmoreno2827 2 місяці тому

    I wanna marry Bob! Thanks

  • @noli-timere-crede-tantum
    @noli-timere-crede-tantum 2 місяці тому

    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)

  • @mpty2022
    @mpty2022 2 місяці тому +5

    thats opposite of what python was developed for