``````通过列表生成式，我们可以直接创建一个列表。但是，受到内存限制，列表容量肯定是有限的。

~~称为生成器（Generator）。~~

>>> a=(x*x for x in range(1,10))
>>> print(a)          #<generator object <genexpr> at 0x00000284C52B19E8>
>>> print(next(a))

>>> a=(x*x for x in range(1,10))
>>> print(a)          #<generator object <genexpr> at 0x00000284C52B19E8>
>>> print(next(a))       #4
>>>print(next(a))       #9
>>>print(next(a))       #16
>>>print(next(a))       #25

generator保存的是算法，每次调用next()，就计算出下一个元素的值，直到计算到最后一个元素，

>>> a=(x*x for x in range(1,10))
>>> for i in a:
>>>     print(i)            #1 4 9 16 25 36 49 64 81

generator非常强大。如果推算的算法比较复杂，用类似列表生成式的for循环无法实现的时候，

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

>>> def fib(max):
>>>     n, a, b = 0, 0, 1
>>>     while n < max:
>>>         print(b)
>>>         a, b = b, a + b
>>>         n = n + 1
>>> fib(9)                         #1, 1, 2, 3, 5, 8, 13, 21, 34

>>> def fib(max):
>>>     n, a, b = 0, 0, 1
>>>     while n < max:
>>>         yield(b)
>>>         a, b = b, a + b
>>>         n = n + 1
>>> for i in fib(9):
>>>     print(i)                      #1, 1, 2, 3, 5, 8, 13, 21, 34

>>> def odd():
>>>     print('step 1')
>>>     yield 1
>>>     print ('step 2')
>>>     yield 3
>>>     print ('step 3')
>>>     yield 5

>>> o = odd()
>>> print(next(o))              #step 1  ,  1
>>> print(next(o))              #step 2  ,  3
>>> print(next(o))              #step 3  ,  5
>>> print(next(o))  #StopIteration

>>> def odd():
>>>     print('step 1')
>>>     yield 1
>>>     print ('step 2')
>>>     yield 3
>>>     print ('step 3')
>>>     yield 5
>>> for i in odd():
>>>     print(i)  #step 1,1   step 2,3  step 3,5

generator是非常强大的工具，在Python中，可以简单地把列表生成式改成generator，

for循环随之结束。``````