Understanding generators in Python

前端 未结 12 1797
日久生厌
日久生厌 2020-11-21 05:46

I am reading the Python cookbook at the moment and am currently looking at generators. I\'m finding it hard to get my head round.

As I come from a Java background, i

12条回答
  •  梦如初夏
    2020-11-21 06:21

    First of all, the term generator originally was somewhat ill-defined in Python, leading to lots of confusion. You probably mean iterators and iterables (see here). Then in Python there are also generator functions (which return a generator object), generator objects (which are iterators) and generator expressions (which are evaluated to a generator object).

    According to the glossary entry for generator it seems that the official terminology is now that generator is short for "generator function". In the past the documentation defined the terms inconsistently, but fortunately this has been fixed.

    It might still be a good idea to be precise and avoid the term "generator" without further specification.

提交回复
热议问题