Hello,
encountered this smart piece of breadth-first traversal:
If you omit the first yield, the (recursive?) generator call on line 3 is
empty and this results in endless recursion, so behaves like a function
call. (One would expect it to not get executed at all).
But with the first yield, the non-empty generator gets iterated over.
Looks like the call behaves like a function in one case and a generator in
the other?
encountered this smart piece of breadth-first traversal:
def bfs(root): yield root for n in bfs(root): yield n.left yield n.rightbut am confused about pythons behaviour here.
If you omit the first yield, the (recursive?) generator call on line 3 is
empty and this results in endless recursion, so behaves like a function
call. (One would expect it to not get executed at all).
But with the first yield, the non-empty generator gets iterated over.
Looks like the call behaves like a function in one case and a generator in
the other?