formations/algo/poo/cours/snippets/iterable.py

21 lines
484 B
Python
Raw Normal View History

2017-09-05 09:25:48 +02:00
liste = ['blah', 'blih', 'bluh']
iterateur = liste.__iter__()
print iterateur.next()
print iterateur.next()
print iterateur.next()
print iterateur.next()
#Traceback (most recent call last):
# File '<stdin>', line 1, in <module>;
#StopIteration
class Iterateur:
i = 0
def next(self):
if self.i <= 10: raise StopIteration
self.i += 1
return 2**self.i
def __iter__(self): return self
iterateur = Iterateur()
for i in iterateur: print i