Z
Z
zlodiak2019-10-18 23:20:04
Python
zlodiak, 2019-10-18 23:20:04

Were you able to show the differences between a generator and an iterator?

Before the interview, I would like to understand if I am confused in terminology. Please help with criticism.
Do I understand correctly that:
1. it should be clearly understood that there are concepts: an iterator, a generator, an iterable object
2. a generator is a special case of an iterator
3. a generator and an iterator implement the iterator interface. That is, they have methods __iter__(), __next__()
4. an iterable object has a method __iter__() that returns an iterator or generator
An example of an iterator to point 3 is here .
An example of a generator for point 3 is here .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2019-10-19
@planc

the whole point is this:

from time import sleep


def my_g():
    for i in range(5):
        sleep(1)
        yield i


def my_i():
    ar = []
    for i in range(5):
        sleep(1)
        ar.append(i)
    return ar


print("generator:")
for i in my_g():
    print(i)


print("iterator:")
for i in my_i():
    print(i)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question