These functions check the iteration protocol of a Python object
when accessed from R through reticulate
.
Details
py_is_iterable()
returnsTRUE
if the object can return an iterator via Python'siter()
function, otherwiseFALSE
.py_is_iterator()
returnsTRUE
if the object itself is an iterator, i.e. an instance ofcollections.abc.Iterator
.
See also
Python's iterator protocol
Examples
if (FALSE) { # \dontrun{
np <- reticulate::import("numpy", convert = FALSE)
# A Python list is iterable but not an iterator
lst <- reticulate::r_to_py(list(1, 2, 3))
py_is_iterable(lst) # TRUE
py_is_iterator(lst) # FALSE
# An iterator (e.g., from iter()) is both iterable and an iterator
it <- py_builtins$iter(lst)
py_is_iterable(it) # TRUE
py_is_iterator(it) # TRUE
} # }