生成器对象

生成器对象是Python用来实现生成器迭代器的对象。它们通常通过迭代产生值的函数来创建,而不是显式调用 PyGen_New()PyGen_NewWithQualName()

type PyGenObject

用于生成器对象的C结构体。

type PySendResult

The enum value used to represent different results of PyGen_Send().

PyTypeObject PyGen_Type

与生成器对象对应的类型对​​象。

int PyGen_Check(PyObject *ob)

如果 ob 是一个生成器对象则返回真值;ob 必须不为 NULL

int PyGen_CheckExact(PyObject *ob)

如果 ob 的类型为 PyGen_Type 则返回真值;ob 必须不为 NULL

PyObject *PyGen_New(PyFrameObject *frame)

基于 frame 对象创建并返回一个新的生成器对象。 此函数会取走一个对 frame 的引用。 参数必须不为 NULL

PyObject *PyGen_NewWithQualName(PyFrameObject *frame, PyObject *name, PyObject *qualname)

基于 frame 对象创建并返回一个新的生成器对象,其中 __name____qualname__ 设为 namequalname。 此函数会取走一个对 frame 的引用。 frame 参数必须不为 NULL

PySendResult PyGen_Send(PyGenObject *gen, PyObject *arg, PyObject **presult)

Sends the arg value into the generator gen. Coroutine objects are also allowed to be as the gen argument but they need to be explicitly casted to PyGenObject*. Returns:

  • PYGEN_RETURN if generator returns. Return value is returned via presult.

  • PYGEN_NEXT if generator yields. Yielded value is returned via presult.

  • PYGEN_ERROR if generator has raised and exception. presult is set to NULL.