What makes an iterator an iterator?

7stud bbxx789_05ss at yahoo.com
Thu Apr 19 23:39:48 CEST 2007


On Apr 19, 3:12 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
> On Apr 19, 5:37 am, Steve Holden <s... at holdenweb.com> wrote:
>
>
>
> > It's nothing to do with the name lookup. Alex mentioned that to remind
> > us that the magic "double-under" names are looked up on the type rather
> > than the instance...
>
> P.next()
>
> vs.
>
> type(P).next()
>
> Where is the "double-under" name?

The following example looks up next() in the instance not the class:

class Parrot(object):
    def __init__(self):
        self.next = lambda: "hello"
    def __iter__(self):
        return self
    def next(self):
        yield "goodbye"
        raise StopIteration

p = Parrot()
_t = iter(p)
print _t.next()

-----output: ----
hello




More information about the Python-list mailing list