On Thu, Oct 08, 2015 at 12:03:28PM +0100, Oscar Benjamin wrote: > On 8 October 2015 at 01:47, Steven D'Aprano <st...@pearwood.info> wrote: > > In 3.3, you will have a problem that FrozenDict is not a proper > > iterator. You can't set self.__next__ = self.next, that won't work. > > Dunder methods have to be on the class, not on the instance, so instead > > of making the assignment in the __init__ method, put this in the body of > > your class: > > > > def next(self): > > # Python 2 method > > ... > > > > __next__ = next # Python 3 method. > > > > > > Unfortunately that's not enough to get it working in Python 3. I need > > more time to think about that. > > There shouldn't be a __next__ method on the FrozenDict class. __iter__ > should return a distinct iterator. Albert has already fixed this by > using: > > def __iter__(self): > return iter(self.__kwargs)
That's one solution, but it is certainly possible for the class to be its own iterator, in which case it needs to follow two rules: (1) self.__next__() needs to return the next value, or raise StopIteration; (2) self.__iter__() needs to return self; and of course like all dunder methods __next__ and __iter__ need to be defined on the class itself, not on the instance. Just returning iter(values) is probably much easier, but not as much fun :-) -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor