Monday, 12 October 2015

GeneraTORs-PYthON

Generator is a special kind of iterator.

Any generator also is an iterator not vice versa.
Any generator therefore is a factory that lazily produces values.
Here is an example:fibonacci series using generators
>>> def fib():
...     prev, curr = 0, 1
...     while True:
...         yield curr
...         prev, curr = curr, prev + curr
...
>>> f = fib()
>>> list(islice(f, 0, 10))
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

No comments:

Post a Comment