Iterable Objects
Classes that provide begin() and end() methods are automatically iterable in Python.
// Iterable objects
#include <c2py/c2py.hpp>
#include <vector>
namespace mylib {
class range {
std::vector<int> values_;
public:
range(int n) {
for (int i = 0; i < n; ++i) values_.push_back(i);
}
// Provide begin() and end() for iteration
auto begin() const { return values_.begin(); }
auto end() const { return values_.end(); }
};
} // namespace mylib
The Python usage:
from mymodule import Range
r = Range(5)
for i in r:
print(i) # 0, 1, 2, 3, 4