[Python-ideas] PEP 380 alternative: A yielding function

Anders J. Munch 2010 at jmunch.dk
Wed Jul 28 18:31:51 CEST 2010

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

More information about the Python-ideas mailing list