On Sat, Jan 18, 2014 at 4:22 AM, Chris “Kwpolska” Warrick <kwpol...@gmail.com> wrote: > For Python 2, use xrange() instead to get an iterator. In Python 3, > range() is already an iterator.
`xrange` and 3.x `range` aren't iterators. They're sequences. A sequence implements `__len__` and `__getitem__`, which can be used to implement an iterator, reversed iterator, and the `in` operator (i.e. `__contains__`). class Sequence: def __len__(self): return 3 def __getitem__(self, k): if k > 2: raise IndexError return k >>> seq = Sequence() >>> len(seq) 3 >>> type(iter(seq)) <class 'iterator'> >>> list(seq) [0, 1, 2] >>> type(reversed(seq)) <class 'reversed'> >>> list(reversed(seq)) [2, 1, 0] >>> 0 in seq True >>> 3 in seq False `xrange` and Py3 `range` both implement the following special methods: __len__ __getitem__ __iter__ __reversed__ 3.x `range` also implements: __contains__ index count Neither supports sequence concatenation or repetition. Both are manually registered as a subclass of the abstract class `collections.Sequence`. >>> issubclass(range, collections.Sequence) True http://docs.python.org/3/library/collections.abc _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor