Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _sentinel=object()
- # like the tp_iter slot on new-style classes
- # http://hg.python.org/cpython/file/c6e529c1f87c/Objects/typeobject.c#l5907
- def _tp_iter(object):
- typ = type(object)
- __iter__ = getattr(typ, '__iter__', _sentinel)
- if __iter__ is not _sentinel:
- return __iter__()
- __getitem__ = getattr(type, '__getitem__', _sentinel)
- if __getitem__ is _sentinel:
- raise TypeError("'%.200s' object is not iterable' % typ.__name__)
- return SequenceIterator(object)
- # like the PyObject_GetIter function
- # http://hg.python.org/cpython/file/c6e529c1f87c/Objects/abstract.c#l2644
- def _iter(object):
- it = _tp_iter(object)
- if not isinstance(it, Iterable):
- raise TypeError("iter() returned non-iterator of type '%.100s'" % type(it).__name__)
- return it
- # like the actual tier function
- # http://hg.python.org/cpython/file/c6e529c1f87c/Python/bltinmodule.c#l1290
- def iter(object, sentinel=_sentinel):
- if sentinel is _sentinel:
- return _iter(object)
- if not callable(object):
- raise TypeError("iter(v, w): v must be callable")
- return CallableIterator(object, sentinel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement