iter() and iterators
-
Collection constructors and synchronous native consumers such as
sum,sortedanditertoolsadaptors cannot suspend in user__iter__or__next__methods, or in callbacks reached throughmapandfilter. An external or OS call raisesNotImplementedErrorinside the method or callback, where it can be caught. This restriction does not apply tofor, comprehensions, directiter()/next(), orfunctools.reduce(). -
iter(callable, sentinel)runscallablesynchronously, so one that calls an external/OS function cannot suspend and raisesNotImplementedError, likesorted(key=...). -
iter(callable, sentinel)comparesresult == sentinel, where CPython comparessentinel == result; only observable if the two sides have asymmetric__eq__. -
The vendored type stub is upstream typeshed’s verbatim, so
-tacceptsiter(obj)for an object with only__getitem__; Monty has no__getitem__iteration fallback and raisesTypeErrorat runtime. -
-tacceptsfor x in obj(though nota, b = obj) for a class that opts out of iteration with__iter__ = None, which raisesTypeErrorat runtime as it does in CPython. -
Built-in iterators do not expose their dunders as attributes:
hasattr(iter([1]), '__iter__')isFalse, where CPython reportsTrue. Iteration itself works; only attribute lookup of the dunder differs. This covers every built-in iterator, includingmap,filterand theitertoolsadaptors. -
Iterator-specific attributes such as
__length_hint__are not exposed.