What is the difference between a generator and an array?

后端 未结 4 693
面向向阳花
面向向阳花 2021-01-31 15:35

Today the PHP team released the PHP 5.5.0 version, which includes support for generators. Reading the documentation, I noticed that it does exactly what it coul

4条回答
  •  梦毁少年i
    2021-01-31 15:57

    As in Python:

    When an iteration over a set of item starts using the for statement, the generator is run. Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn.

    ...Generators executes the yield statements one at a time, pausing in between to yield execution back to the main for loop.

    -learnpython.org

提交回复
热议问题