On Thu, Jan 23, 2014 at 2:24 PM, Keith Winston <keithw...@gmail.com> wrote: > On Thu, Jan 23, 2014 at 7:05 AM, eryksun <eryk...@gmail.com> wrote: >> Generally you'll make `__iter__` a generator, so you don't have to >> worry about implementing `__next__`. Also, the built-in function >> `next` was added in 2.6, so you don't have to worry about the method >> name difference between 2.x and 3.x, either. > > I'm now realizing I don't understand this comment at all. First, there > IS a __iter__ method in the example: does the presence of such make it > a generator already, or is it a usage thing, or something else? I > don't yet completely understand the difference/point... or maybe you > mean inherit the generator class?
Sorry, sloppy language. You'll make `__iter__` a generator *function* that returns a generator, which is an iterator (i.e. has a `__next__` method). This works in 2.6+ and 3.x without needing any kludges or 2to3. For example: class MyIter: def __init__(self, thelist): self.thelist = thelist def __iter__(self): for item in self.thelist: yield item my_iter = MyIter(['a', 'b']) it = iter(my_iter) >>> type(it) <class 'generator'> >>> next(it) 'a' >>> next(it) 'b' >>> list(it) # exhausted [] > Second: you state that I don't have to worry about the name > difference, but when I changed the method name from next to __next__ > it worked in 3.3. So what's your point here? The 2nd statement in my original comment was contingent on the first, which I apparently failed to clarify in the followup. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor