Class PyIterator

java.lang.Object
org.python.core.PyObject
org.python.core.PyIterator
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
PyCallIter, PyEnumerate, PyGenerator, PySequenceIter

public abstract class PyIterator extends PyObject
An abstract helper class usefull when implementing an iterator object. This implementation supply a correct __iter__() and a next() method based on the __iternext__() implementation. The __iternext__() method must be supplied by the subclass. If the implementation raises a StopIteration exception, it should be stored in stopException so the correct exception can be thrown to preserve the line numbers in the traceback.
See Also:
  • Field Details

    • __doc__next

      public static PyString __doc__next
  • Constructor Details

    • PyIterator

      public PyIterator()
  • Method Details

    • __iter__

      public PyObject __iter__()
      Description copied from class: PyObject
      Return an iterator that is used to iterate the element of this sequence. From version 2.2, this method is the primary protocol for looping over sequences.

      If a PyObject subclass should support iteration based in the __finditem__() method, it must supply an implementation of __iter__() like this:

          public PyObject __iter__() {
              return new PySequenceIter(this);
          }
       
      When iterating over a python sequence from java code, it should be done with code like this:
          PyObject iter = seq.__iter__();
          for (PyObject item; (item = iter.__iternext__()) != null;)  {
              // Do somting with item
          }
       
      Overrides:
      __iter__ in class PyObject
    • next

      public PyObject next()
    • __iternext__

      public abstract PyObject __iternext__()
      Description copied from class: PyObject
      Return the next element of the sequence that this is an iterator for. Returns null when the end of the sequence is reached.
      Overrides:
      __iternext__ in class PyObject