| Did you know ... | Search Documentation: |
| Predicate py_iter/2 |
[nondet]py_iter(+Iterator,
-Value)
Obj:Attr = Value
construct is not accepted.__iter__ on the result to get the iterator itself.__next__ function of the iterator.
The example below uses the built-in iterator range():
?- py_iter(range(1,3), X). X = 1 ; X = 2.
Note that the implementation performs a look ahead, i.e., after successful unification it callsānext()` again. On failure the Prolog predicate succeeds deterministically. On success, the next candidate is stored.
Note that a Python generator is a Python iterator.
Therefore, given the Python generator expression below, we can use
py_iter(squares(1,5),X) to generate the squares on
backtracking.
def squares(start, stop):
for i in range(start, stop):
yield i * i
| Options | is processed as with py_call/3. |
query(), i.e.,
it is not possible to iterate over a Python iterator that under the
hoods relies on a Prolog non-deterministic predicate.