__next__ in generators and iterators and what is a method-wrapper?

后端 未结 3 1177
天涯浪人
天涯浪人 2020-12-15 12:49

I was reading about generator and iterators and the role of __next__() .

\'__next__\' in dir(mygen). is true

\'__next__\' in

3条回答
  •  萌比男神i
    2020-12-15 13:15

    __next__ and __iter__ are method wrappers for when you do next(some_gen) or iter(some_sequence). next(some_gen) is the same as some_gen.__next__()

    So if I do mygen = iter(mylist) then mygen is mylist implemented as a generator object and has a __next__ method descriptor. Lists themselves do not have this method because they are not generators.

    Generators are iterators. Check out difference between generators and iterators

提交回复
热议问题