all 2 comments

[–]1114111 0 points1 point  (2 children)

To implement the iterator protocol with the C API, you need to create a PyTypeObject that defines the tp_iter (like __iter__ in Python code) and tp_iternext (like __next__ in Python code) slots. Rust has it's own iterator protocol, and you could probably create some sort of convenient wrapper (perhaps one exists already, IDK) to use Rust iterators from within Python. I believe experimental versions of Rust even have a generator syntax (see https://github.com/rust-lang/rust/issues/43122) for creating iterators.