From 867a9e124f9c8be88664680cba50b4448d8148fc Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 16 Aug 2009 19:30:05 -0400 Subject: [PATCH] mention for x in seq --- special-method-names.html | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/special-method-names.html b/special-method-names.html index 43b9c1b..009b126 100644 --- a/special-method-names.html +++ b/special-method-names.html @@ -90,6 +90,13 @@ h3:before{counter-increment:h3;content:'B.' counter(h2) '.' counter(h3) '. '}
  • The __reversed__() method is uncommon. It takes an existing sequence and returns an iterator that yields the items in the sequence in reverse order, from last to first. +

    As you saw in the Iterators chapter, a for loop can act on an iterator. In this loop: + +

    for x in seq:
    +    print(x)
    + +

    Python 3 will call seq.__iter__() to create an iterator, then call the __next__() method on that iterator to get each value of x. When the __next__() method raises a StopIteration exception, the for loop ends gracefully. +

    Computed Attributes