14. Генераторы и генераторные выражения.

def counter():
    val = 1
    for ind in xrange(10):
        yield val
        val = val + 1

print type(counter), type(counter())


print [val for val in counter()]
<type 'function'> <type 'generator'>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Генераторные выражения

gen = (val for val in [1, 2, 3])
print gen
print [val for val in gen]
print [val for val in gen]
<generator object <genexpr> at 0x7f4d1c6664b0>
[1, 2, 3]
[]