Skip to contents

These functions check the iteration protocol of a Python object when accessed from R through reticulate.

Usage

py_is_iterable(obj)

py_is_iterator(obj)

Arguments

obj

A Python object proxy.

Value

A Boolean scalar (TRUE or FALSE).

Details

  • py_is_iterable() returns TRUE if the object can return an iterator via Python's iter() function, otherwise FALSE.

  • py_is_iterator() returns TRUE if the object itself is an iterator, i.e. an instance of collections.abc.Iterator.

See also

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
} # }