About 15,200,000 results
Open links in new tab
  1. Understanding generators in Python - Stack Overflow

    Python 2.5 added the ability to pass values back in to the generator as well. In doing so, the passed-in value is available as an expression resulting from the yield statement which had …

  2. Difference between Python's Generators and Iterators

    What is the difference between iterators and generators? Some examples for when you would use each case would be helpful.

  3. How can I type hint a generator in Python 3? [duplicate]

    The Iterator, Generator, and Iterable are slightly different in details and carry different type of information, so understanding the difference might help choosing the correct one for your …

  4. What does the "yield" keyword do in Python? - Stack Overflow

    Oct 24, 2008 · Yield in Python used to create a generator function. Generator function behaves like an iterator, which can be used in loop to retrieve items one at a time. When a generator …

  5. What is the purpose of the "send" function on Python generators?

    generator.send(value) Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield expression. The send() method …

  6. python - What can you use generator functions for? - Stack Overflow

    The generator pauses at each yield until the next value is requested. If the generator is wrapping I/O, the OS might be proactively caching data from the file on the assumption it will be …

  7. Python Generator: Reading data from database - Stack Overflow

    Apr 25, 2025 · Just going over a code snippet from this article for generator to read data from database: def generate_dataset(cursor): cursor.execute(query) for row in cursor.fetchall(): ...

  8. python - Using next () on generator function - Stack Overflow

    May 28, 2018 · But in second case you're looping over one generator object and it will continue to consuming the generator until it hit the StopIteration. For a better demonstration you can …

  9. python - How to take the first N items from a generator or list ...

    Slicing a generator import itertools top5 = itertools.islice(my_list, 5) # grab the first five elements You can't slice a generator directly in Python. itertools.islice() will wrap an object in a new …

  10. python - How to write a generator class? - Stack Overflow

    I see lot of examples of generator functions, but I want to know how to write generators for classes. Lets say, I wanted to write Fibonacci series as a class. class Fib: def __init__(self): ...