Posts

Showing posts with the label Pyton Iterator

Introduction to multitasking with Python #003 gevent (2020 tutorial)

Image
  1 . Review of Iterator In python, we could achieve multitasking through another way by using gevent module. Howerver before introducing gevent, we have to review the concept of iterator. You may have used python for loop millions of times: for i in Iterable:     print(i) We have learned several iterable objects, str, list, tuple, dict and set. Those iterable objects or sequences could be used with the for loop. And we take that for granted. But look at the following example: >>> for i in 100: print(i) Traceback (most recent call last):   File "<pyshell#2>", line 1, in <module>     for i in 100: TypeError: 'int' object is not iterable >>>   Why cannot int type be used with for loop in Python and how could you make an object iterable and be used with for loop? Now, let's make an iterable object that could be used with for loop. We could use isinstance() built-in method to check whether an object is iterable or not. >>> fr