Posted by & filed under python.

Определение итератора:

class MyIter:
    def __init__(self, start, stop):        
        self.value = start - 1
        self.stop  = stop
    def __iter__(self):                     
        return self
    def next(self):                         
        self.value += 1
        if self.value > self.stop:
            raise StopIteration        
        return self.value

Работа с итератором:

for i in MyIter (1,5):
        print i
Опубликовать в Facebook
Опубликовать в Google Plus

Leave a Reply

You must be logged in to post a comment.