iterator object : Class iter : Class : Python examples (example source code) Organized by topic

C++
PHP
Python
Python Home »  Class   » [  Class iter  ]  Screenshots 
 



iterator object




class Squares:
    def __init__(self, start, stop):
        self.value = start - 1
        self.stop  = stop
    def __iter__(self):                   # get iterator object
        return self
    def next(self):                       # on each for iteration
        if self.value == self.stop:
            raise StopIteration
        self.value += 1
        return self.value ** 2

for i in Squares(1,5):
     print i,

X = Squares(1,5)


X = Squares(1,5)
print [for n in X]                     # exhausts items

print [for n in X]                     # now it's empty

print [for n in Squares(1,5)]

print list(Squares(1,3))

           
       
Related examples in the same category








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.