Playing around with trees, I stumbled over this behaviour:
def descendants (self):
return #or "pass" or "42"
obviously returns None
.
On the other hand side:
def descendants (self):
return
yield 42
returns a generator which yields nothing (actually the behaviour I needed for leaf nodes).
Could somebody explain to me what is happening under the hood here?
Shouldn't the yield 42
be unreachable code? (I guess the decision whether a function is a generator or a "normal" function is made at compile time, based on whether it contains one or various yield
statements, be they reachable or not. But this is just a shot in the dark.)
The context is the following: I have trees and each node is either a tree or a leaf. Now I want to generate all the descendants of a node:
class Leaf (Node):
@property
def descendants (self):
return
yield 42
class Tree (Node):
@property
def descendants (self):
for child in self.children:
yield child
yield from child.descendants