Advertisement

OOPIterator OOPIterator

阅读量:
复制代码
 class CapitalIterable:

    
     def __init__(self, string):
    
     self.string = string
    
  
    
     def __iter__(self):
    
     return CapitalIterator(self.string)
    
  
    
  
    
 class CapitalIterator:
    
     def __init__(self, string):
    
     self.words = [w.capitalize() for w in string.split()]
    
     self.index = 0
    
  
    
     def __next__(self):
    
     if self.index == len(self.words):
    
         raise StopIteration()
    
  
    
     word = self.words[self.index]
    
     self.index += 1
    
     return word
    
  
    
     def __iter__(s

全部评论 (0)

还没有任何评论哟~