產生器 (Generator) 物件

產生器物件是 Python 用來實現產生器疊代器 (generator iterator) 的物件。它們通常透過疊代會產生值的函式來建立,而不是顯式呼叫 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 的參照 (reference)。引數必須不為 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.

在 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.

在 3.6 版被加入.

int PyAsyncGen_CheckExact(PyObject *op)

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

在 3.6 版被加入.