P_itershould have an__iter__method to fully comply with the iterator protocol:def __iter__(self): return selfDefining the
P_iterclass inside a method hurts readability and makes the class unavailable for anyone who wants to check the type of an iterator object.In
Permutation2.__iter__I would use this approach to avoid thestahpvariable :def __iter__(self): curr = self.start while True: yield curr curr = self.p[curr] if curr == self.start: return