Class PyIterator

java.lang.Object
org.python.core.PyObject
org.python.core.PyIterator
All Implemented Interfaces:
Serializable, Iterable<Object>, Traverseproc
Direct Known Subclasses:
ArrayCData.ArrayIter, chain, combinations, combinationsWithReplacement, compress, count, cStringIO.StringIO, cycle, dropwhile, groupby, ifilter, ifilterfalse, imap, islice, izip, izipLongest, JavaIterator, permutations, product, PyCallIter, PyEnumerate, PyFastSequenceIter, PyGenerator, PyListIterator, PyReader, PyReversedIterator, PySequenceIter, PyTeeIterator, PyTupleIterator, PyXRangeIter, repeat, starmap, takewhile

public abstract class PyIterator extends PyObject implements Iterable<Object>, Traverseproc
An abstract helper class useful 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()
    • PyIterator

      public PyIterator(PyType subType)
  • Method Details

    • __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
    • __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:
       for (PyObject item : seq.asIterable()) {
           // Do something with item
       }
       
      Overrides:
      __iter__ in class PyObject
    • next

      public PyObject next()
      The exposed next method. Note that exposed derivable subclasses of PyIterator should override next to call doNext(custom___iternext__), as __iternext__ is overridden by the Derived classes.
      Returns:
      a PyObject result
    • iterator

      public Iterator<Object> iterator()
      Specified by:
      iterator in interface Iterable<Object>
    • __tojava__

      public Object __tojava__(Class<?> c)
      Description copied from class: PyObject
      Equivalent to the Jython __tojava__ method. Tries to coerce this object to an instance of the requested Java class. Returns the special object Py.NoConversion if this PyObject can not be converted to the desired Java class.
      Overrides:
      __tojava__ in class PyObject
      Parameters:
      c - the Class to convert this PyObject to.
    • traverse

      public int traverse(Visitproc visit, Object arg)
      Description copied from interface: Traverseproc
      Traverses all directly contained PyObjects. Like in CPython, arg must be passed unmodified to visit as its second parameter. If Visitproc.visit(PyObject, Object) returns nonzero, this return value must be returned immediately by traverse. Visitproc.visit(PyObject, Object) must not be called with a null PyObject-argument.
      Specified by:
      traverse in interface Traverseproc
    • refersDirectlyTo

      public boolean refersDirectlyTo(PyObject ob)
      Description copied from interface: Traverseproc
      Optional operation. Should only be implemented if it is more efficient than calling Traverseproc.traverse(Visitproc, Object) with a visitproc that just watches out for ob. Must return false if ob is null.
      Specified by:
      refersDirectlyTo in interface Traverseproc