Python - Iterators and Iterables : What Are They and How Do They Work?
** Iterators and Iterables : What Are They and How Do They Work?
==========================================
nums = [1, 2, 3]
for num in nums:
print(num)
---------------------------------
1
2
3
==========================================
==========================================
nums = [1, 2, 3]
print(dir(nums))
---------------------------------
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__',
'__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__',
'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
'reverse', 'sort']
==========================================
==========================================
nums = [1, 2, 3]
print(next(nums))
---------------------------------
File "C:\Users\purunet\Documents\py9\ii.py", line 7, in <module>
print(next(nums))
TypeError: 'list' object is not an iterator
==========================================
==========================================
nums = [1, 2, 3]
i_nums = nums.__iter__()
print(i_nums)
print(dir(i_nums))
---------------------------------
<list_iterator object at 0x02461D18>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__',
'__sizeof__', '__str__', '__subclasshook__']
==========================================
==========================================
nums = [1, 2, 3]
i_nums = iter(nums)
print(i_nums)
print(dir(i_nums))
---------------------------------
<list_iterator object at 0x00E51D18>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__',
'__sizeof__', '__str__', '__subclasshook__']
==========================================
==========================================
nums = [1, 2, 3]
i_nums = iter(nums)
print(next(i_nums))
---------------------------------
1
==========================================
==========================================
nums = [1, 2, 3]
i_nums = iter(nums)
print(next(i_nums))
print(next(i_nums))
print(next(i_nums))
---------------------------------
1
2
3
==========================================
==========================================
nums = [1, 2, 3]
i_nums = iter(nums)
print(next(i_nums))
print(next(i_nums))
print(next(i_nums))
print(next(i_nums))
---------------------------------
1Traceback (most recent call last):
File "C:\Users\purunet\Documents\py9\ii.py", line 10, in <module>
2
3
print(next(i_nums))
StopIteration
==========================================
==========================================
nums = [1, 2, 3]
i_nums = iter(nums)
while True:
try:
item = next(i_nums)
print(item)
except StopIteration:
break
---------------------------------
1
2
3
==========================================
==========================================
class MyRange:
def __init__(self, start, end):
self.value = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.value >= self.end:
raise StopIteration
current = self.value
self.value += 1
return current
nums = MyRange(1, 10)
for num in nums:
print(num)
---------------------------------
1
2
3
4
5
6
7
8
9
==========================================
==========================================
class MyRange:
def __init__(self, start, end):
self.value = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.value >= self.end:
raise StopIteration
current = self.value
self.value += 1
return current
nums = MyRange(1, 10)
print(next(nums))
---------------------------------
1
==========================================
==========================================
class MyRange:
def __init__(self, start, end):
self.value = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.value >= self.end:
raise StopIteration
current = self.value
self.value += 1
return current
nums = MyRange(1, 10)
print(next(nums))
print(next(nums))
print(next(nums))
print(next(nums))
---------------------------------
1
2
3
4
==========================================
==========================================
class MyRange:
def __init__(self, start, end):
self.value = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.value >= self.end:
raise StopIteration
current = self.value
self.value += 1
return current
def my_range(start, end):
current = start
while current < end:
yield current
current += 1
nums = my_range(1, 10)
print(next(nums))
print(next(nums))
print(next(nums))
print(next(nums))
---------------------------------
1
2
3
4
==========================================
==========================================
class MyRange:
def __init__(self, start, end):
self.value = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.value >= self.end:
raise StopIteration
current = self.value
self.value += 1
return current
def my_range(start, end):
current = start
while current < end:
yield current
current += 1
nums = my_range(1, 10)
for num in nums:
print(num)
---------------------------------
1
2
3
4
5
6
7
8
9
==========================================
==========================================
class MyRange:
def __init__(self, start, end):
self.value = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.value >= self.end:
raise StopIteration
current = self.value
self.value += 1
return current
def my_range(start):
current = start
while True:
yield current
current += 1
nums = my_range(1)
for num in nums:
print(num)
---------------------------------
1에서부터 시작해서, 정지명령을 내릴 때 까지 계속 1씩 증가.
==========================================