Python: nested iterator

Just a (probably) useful examle of nested Python generator written in Python using recursive *yield* expression.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

sample = (1, 2, (3, 4, 5), 6, ((7, ), 8), 9)

def cycle(smth):
    for i in smth:
        if isinstance(i, int):
            yield i**2
        else:
            for j in cycle(i):
                yield j

for j in cycle(sample):
    print(j)

-------------

1
4
9
16
25
36
49
64
81

Comments

Comments powered by Disqus