This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| my_list = [1, 2, 3, 4, 5] | |
| print(my_list) | |
| # [1, 2, 3, 4, 5] | |
| my_list_iterator = iter(my_list) | |
| print(my_list_iterator) | |
| # <list_iterator object at 0x7f048a4b67f0> | |
| next(my_list_iterator) | |
| # 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections.abc import Iterable, Iterator | |
| my_list = [1, 2, 3, 4, 5] | |
| my_list_iterator = iter(my_list) | |
| isinstance(my_list,Iterable) | |
| # True | |
| isinstance(my_list,Iterator) | |
| # False | |
| isinstance(my_list_iterator,Iterable) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections.abc import Iterable, Iterator | |
| my_list = [1, 2, 3, 4, 5] | |
| my_list_iterator = iter(my_list) | |
| isinstance(my_list,Iterable) | |
| #True | |
| isinstance(my_list,Iterator) | |
| #False | |
| isinstance(my_list_iterator,Iterable) | |
| #True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections.abc import Iterable, Iterator | |
| class Fib(object): | |
| def __init__(self): | |
| self.a, self.b = 0, 1 | |
| def __iter__(self): | |
| return self | |
| def __next__(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections.abc import Iterable, Iterator | |
| def Fib(max): | |
| n, a, b = 0, 0, 1 | |
| while n < max: | |
| yield b | |
| a, b = b, a + b | |
| n = n + 1 | |
| return 'done' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # convert an Iterable to an Iterator | |
| my_list_iterator = iter(my_list) | |
| while True: | |
| try: | |
| # get the next item | |
| element = next(my_list_iterator) | |
| except StopIteration: | |
| # if StopIteration is raised, stop for loop | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| my_list = [i for i in range(8)] | |
| my_generator = (i for i in range(8)) | |
| print(my_list) | |
| print(my_generator) | |
| # [0, 1, 2, 3, 4, 5, 6, 7] | |
| # <generator object <genexpr> at 0x7f8fc3ec9a40> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def my_generator(maximum): | |
| n = 0 | |
| while n < maximum: | |
| n += 1 | |
| yield n | |
| return 'Done' | |
| g = my_generator(maximum=5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def my_generator(maximum): | |
| n = 0 | |
| while n < maximum: | |
| yield n | |
| return 'Done' | |
| print(type(my_generator)) | |
| # <class 'function'> | |
| print(type(my_generator(5))) | |
| # <class 'generator'> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def example(): | |
| print('step 1') | |
| yield 1 | |
| print('step 2') | |
| yield 2 | |
| print('step 3') | |
| yield 3 | |
| g = example() |