Skip to main content
added 305 characters in body
Source Link
Janne Karila
  • 10.7k
  • 21
  • 34
  • P_iter should have an __iter__ method to fully comply with the iterator protocol:

     def __iter__(self):
         return self
    
  • Defining the P_iter class 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 the stahp variable :

     def __iter__(self):
         curr = self.start
         while True:
             yield curr
             curr = self.p[curr]
             if curr == self.start:
                 return
    
  • P_iter should have an __iter__ method to fully comply with the iterator protocol:

     def __iter__(self):
         return self
    
  • Defining the P_iter class inside a method hurts readability and makes the class unavailable for anyone who wants to check the type of an iterator object.

  • P_iter should have an __iter__ method to fully comply with the iterator protocol:

     def __iter__(self):
         return self
    
  • Defining the P_iter class 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 the stahp variable :

     def __iter__(self):
         curr = self.start
         while True:
             yield curr
             curr = self.p[curr]
             if curr == self.start:
                 return
    
Source Link
Janne Karila
  • 10.7k
  • 21
  • 34

  • P_iter should have an __iter__ method to fully comply with the iterator protocol:

     def __iter__(self):
         return self
    
  • Defining the P_iter class inside a method hurts readability and makes the class unavailable for anyone who wants to check the type of an iterator object.