Reported by scoder on 19 Nov 2011 15:11 UTC
Python generators have a feature that presents a length hint to interested parties that can take advantage of it, such as "list()". Cython generator expressions should propagate the length hint of the underlying iterator if there is only one "for" loop and no "if" selector predicate. The C-API function to ask for a length hint is _PyObject_LengthHint(), which is available from Py2.5 on. Generator expressions would then need to provide a .__length_hint__() special method that returns either a constant result (when known at compile time, e.g. for sliced C arrays), or calls _PyObject_LengthHint() on the underlying iterator.
Reported by scoder on 19 Nov 2011 15:11 UTC Python generators have a feature that presents a length hint to interested parties that can take advantage of it, such as "list()". Cython generator expressions should propagate the length hint of the underlying iterator if there is only one "for" loop and no "if" selector predicate. The C-API function to ask for a length hint is
_PyObject_LengthHint()
, which is available from Py2.5 on. Generator expressions would then need to provide a.__length_hint__()
special method that returns either a constant result (when known at compile time, e.g. for sliced C arrays), or calls_PyObject_LengthHint()
on the underlying iterator.Migrated-From: http://trac.cython.org/ticket/756