ਇਟਰੇਟਰ ਪ੍ਰੋਟੋਕੋਲ¶
ਇਟਰੇਟਰਾਂ ਨਾਲ ਕੰਮ ਕਰਨ ਲਈ ਖ਼ਾਸ ਤੌਰ 'ਤੇ ਦੋ ਫੰਕਸ਼ਨ ਹਨ।
-
int PyIter_Check(PyObject *o)¶
- Part of the Stable ABI since version 3.8.
Return non-zero if the object o can be safely passed to
PyIter_Next(), and0otherwise. This function always succeeds.
-
int PyAIter_Check(PyObject *o)¶
- Part of the Stable ABI since version 3.10.
ਜੇ ਆਬਜੈਕਟ o
AsyncIteratorਪ੍ਰੋਟੋਕੋਲ ਪ੍ਰਦਾਨ ਕਰਦਾ ਹੈ ਤਾਂ ਗੈਰ-ਜ਼ੀਰੋ ਵਾਪਸ ਕਰੋ, ਨਹੀਂ ਤਾਂ0ਵਾਪਸ ਕਰੋ। ਇਹ ਫੰਕਸ਼ਨ ਹਮੇਸ਼ਾਂ ਸਫਲ ਹੁੰਦਾ ਹੈ।Added in version 3.10.
-
PyObject *PyIter_Next(PyObject *o)¶
- Return value: New reference. Part of the Stable ABI.
ਇਟਰੇਟਰ o ਤੋਂ ਅਗਲਾ ਮੁੱਲ ਵਾਪਸ ਕਰੋ।
PyIter_Check()ਅਨੁਸਾਰ ਇਹ ਆਬਜੈਕਟ ਇੱਕ ਇਟਰੇਟਰ ਹੋਣਾ ਚਾਹੀਦਾ ਹੈ (ਇਸ ਦੀ ਜਾਂਚ ਕਰਨਾ ਕਾਲਰ ਦੀ ਜ਼ਿੰਮੇਵਾਰੀ ਹੈ)। ਜੇ ਕੋਈ ਮੁੱਲ ਬਾਕੀ ਨਹੀਂ ਹੈ, ਤਾਂ ਬਿਨਾਂ ਕਿਸੇ ਅਪਵਾਦ ਨੂੰ ਸੈੱਟ ਕੀਤੇNULLਵਾਪਸ ਕੀਤਾ ਜਾਂਦਾ ਹੈ। ਜੇ ਆਈਟਮ ਪ੍ਰਾਪਤ ਕਰਦੇ ਸਮੇਂ ਕੋਈ ਗਲਤੀ ਆਉਂਦੀ ਹੈ, ਤਾਂNULLਵਾਪਸ ਕੀਤਾ ਜਾਂਦਾ ਹੈ ਅਤੇ ਅਪਵਾਦ ਅੱਗੇ ਭੇਜਿਆ ਜਾਂਦਾ ਹੈ।
To write a loop which iterates over an iterator, the C code should look something like this:
PyObject *iterator = PyObject_GetIter(obj);
PyObject *item;
if (iterator == NULL) {
/* propagate error */
}
while ((item = PyIter_Next(iterator))) {
/* do something with item */
...
/* release reference when done */
Py_DECREF(item);
}
Py_DECREF(iterator);
if (PyErr_Occurred()) {
/* propagate error */
}
else {
/* continue doing useful work */
}
-
type PySendResult¶
PyIter_Send()ਦੇ ਵੱਖ-ਵੱਖ ਨਤੀਜਿਆਂ ਨੂੰ ਦਰਸਾਉਣ ਲਈ ਵਰਤਿਆ ਜਾਣ ਵਾਲਾ enum ਮੁੱਲ।Added in version 3.10.
-
PySendResult PyIter_Send(PyObject *iter, PyObject *arg, PyObject **presult)¶
- Part of the Stable ABI since version 3.10.
arg ਮੁੱਲ ਨੂੰ ਇਟਰੇਟਰ iter ਵਿੱਚ ਭੇਜਦਾ ਹੈ। ਇਹ ਹੇਠਾਂ ਦਿੱਤੇ ਮੁੱਲਾਂ ਵਿੱਚੋਂ ਇੱਕ ਵਾਪਸ ਕਰਦਾ ਹੈ:
ਜੇ ਇਟਰੇਟਰ return ਕਰਦਾ ਹੈ ਤਾਂ
PYGEN_RETURN। ਵਾਪਸੀ ਮੁੱਲ presult ਰਾਹੀਂ ਵਾਪਸ ਕੀਤਾ ਜਾਂਦਾ ਹੈ।ਜੇ ਇਟਰੇਟਰ yield ਕਰਦਾ ਹੈ ਤਾਂ
PYGEN_NEXT। yield ਕੀਤਾ ਗਿਆ ਮੁੱਲ presult ਰਾਹੀਂ ਵਾਪਸ ਕੀਤਾ ਜਾਂਦਾ ਹੈ।PYGEN_ERRORif iterator has raised and exception. presult is set toNULL.
Added in version 3.10.