Recursively calling an object method that returns an iterator of itself

Viewed 1374

I'm currently writing a project that requires third party code that uses a method that returns an iterator of itself, an example of how this would look in my code:

def generate():
    for x in obj.children():
        for y in x.children():
            for z in y.children():
                yield z.thing

Currently this simply clutters my code, and becomes hard to read after 3 levels. Ideally I'd get it to do something like this:

x = recursive(obj, method="children", repeat=3).thing

Is there a built in way to do this in Python?

3 Answers
Related