28 Jul
2010
28 Jul
'10
12:31 p.m.
Carl M. Johnson wrote:
What would this code do:
def yield_if_true(x): if x: yield_(x)
def maybe_yield(x): if calculate_property(x): yield_if_true(x) else: return None
maybe_yield(5)
maybe_yield is not a generator, because it does not use the yield keyword. Nothing changed about that. As there's no generator here, yield_(x) would raise some exception: "RuntimeError: No generator found for yield_" Regular yield syntax remains the preferred option - with yield_ as a supplement for when delegation is needed. Perhaps a better name for it would be 'yield_outer' or 'nonlocal_yield'. regards, Anders