生成器对象

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

type PyGenObject

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

PyTypeObject PyGen_Type

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

int PyGen_Check(PyObject *ob)

如果 ob 是一个 generator 对象则返回真值;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

PyCodeObject *PyGen_GetCode(PyGenObject *gen)

Return a new strong reference to the code object wrapped by gen. This function always succeeds.

Asynchronous Generator Objects

参见

PEP 525

PyTypeObject PyAsyncGen_Type

The type object corresponding to asynchronous generator objects. This is available as types.AsyncGeneratorType in the Python layer.

Added in version 3.6.

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

Create a new asynchronous generator wrapping frame, with __name__ and __qualname__ set to name and qualname. frame is stolen by this function and must not be NULL.

On success, this function returns a strong reference to the new asynchronous generator. On failure, this function returns NULL with an exception set.

Added in version 3.6.

int PyAsyncGen_CheckExact(PyObject *op)

Return true if op is an asynchronous generator object, false otherwise. This function always succeeds.

Added in version 3.6.