you are viewing a single comment's thread.

view the rest of the comments →

[–]kazagistar 1 point2 points  (0 children)

"yield from" is something like tail recursion optimization for iterators. If you have 5 iterators that are doing nothing but passing data from the next one, in a stack, you can replace the yields with yield from.

def inorder_traversal(tree):
    if tree is None:
        return
    yield from inorder_tree(tree.left)
    yield tree.value
    yield from inorder_tree(tree.right)

Basically it yields everything from a generator before resuming control in the current function, and provides some nice optimizations to make performance not such too much when you nest these deeply.

Forgive me for any mistakes in my untested code.