Does enumerate() produce a generator object?

前端 未结 5 919
温柔的废话
温柔的废话 2020-12-06 03:55

As a complete Python newbie, it certainly looks that way. Running the following...

x = enumerate([\'fee\', \'fie\', \'foe\'])
x.next()
# Out[1]: (0, \'fee\')         


        
5条回答
  •  半阙折子戏
    2020-12-06 04:31

    Is it in some sense "generator-like", but not an actual generator?

    Yes, it is. You shouldn't really care if it is a duck, but only if it walks, talks, and smells like one. It just as well be a generator, shouldn't make a real difference.

    It is typical to have generator-like types instead of actual generators, when you want to extend the functionality. E.g. range is also generator-like, but it also supports things like y in range(x) and len(range(x)) (xrange in python2.x).

提交回复
热议问题