You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
490 B
24 lines
490 B
11 years ago
|
# user defined iterator used in something other than a for loop
|
||
|
|
||
|
class MyStopIteration(StopIteration):
|
||
|
pass
|
||
|
|
||
|
class myiter:
|
||
|
def __init__(self, i):
|
||
|
self.i = i
|
||
|
|
||
|
def __iter__(self):
|
||
|
return self
|
||
|
|
||
|
def __next__(self):
|
||
|
if self.i == 0:
|
||
|
raise StopIteration
|
||
|
elif self.i == 1:
|
||
|
raise StopIteration(1)
|
||
|
elif self.i == 2:
|
||
|
raise MyStopIteration
|
||
|
|
||
|
print(list(myiter(0)))
|
||
|
print(list(myiter(1)))
|
||
|
print(list(myiter(2)))
|