0

Consider the following three functions:

def return_iter(iterable):
    return iter(iterable)

def for_loop_yield(iterable):
    for item in iterable:
        yield item

def yield_from_iter(iterable):
    yield from iterable

All of them produce the same results when looped over:

>>> prog_langs = ['python', 'java', 'c++']
>>> for lang in return_iter(prog_langs):
...     print(lang)
python
java
c++
>>> for lang in for_loop_yield(prog_langs):
...     print(lang)
python
java
c++
>>> for lang in yield_from_iter(prog_langs):
...     print(lang)
python
java
c++

What is the difference through?

user141240
  • 253
  • 1
  • 2
  • 6
  • maybe this can help https://stackoverflow.com/questions/2776829/difference-between-pythons-generators-and-iterators – ChatterOne Jun 04 '20 at 08:28
  • Does this answer your question? [In practice, what are the main uses for the new "yield from" syntax in Python 3.3?](https://stackoverflow.com/questions/9708902/in-practice-what-are-the-main-uses-for-the-new-yield-from-syntax-in-python-3) – norok2 Jun 04 '20 at 08:47

0 Answers0