April 2018
Intermediate to advanced
408 pages
10h 42m
English
We noted that there are some limitations of generator expressions and generator functions. The limitations can be observed by executing the following command snippet:
>>> from ch02_ex4 import * >>> pfactorsl(1560) <generator object pfactorsl at 0x1007b74b0> >>> list(pfactorsl(1560)) [2, 2, 2, 3, 5, 13] >>> len(pfactorsl(1560)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'generator' has no len()
In the first example, we saw the generator function, pfactors1, created a generator. The generator is lazy, and doesn't have a proper value until we consume the results yielded by the generator. in itself isn't a limitation; lazy evaluation is an important ...