How to yield results from a nested generator function?

You may have to use the new yield from, available since Python 3.3, known as “delegated generator”. If I understood the question correctly, I came to the same issue, and found an answer elsewhere. I wanted to do something like this: def f(): def g(): do_something() yield x … yield y do_some_other_thing() yield a … … Read more

How to use ‘yield’ inside async function?

Upd: Starting with Python 3.6 we have asynchronous generators and able to use yield directly inside coroutines. import asyncio async def async_generator(): for i in range(3): await asyncio.sleep(1) yield i*i async def main(): async for i in async_generator(): print(i) loop = asyncio.get_event_loop() try: loop.run_until_complete(main()) finally: loop.run_until_complete(loop.shutdown_asyncgens()) # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.shutdown_asyncgens loop.close() Old answer for Python 3.5: … Read more

yield in list comprehensions and generator expressions

Note: this was a bug in the CPython’s handling of yield in comprehensions and generator expressions, fixed in Python 3.8, with a deprecation warning in Python 3.7. See the Python bug report and the What’s New entries for Python 3.7 and Python 3.8. Generator expressions, and set and dict comprehensions are compiled to (generator) function … Read more

Ruby: Proc#call vs yield

I think the first one is actually a syntactic sugar of the other. In other words there is no behavioural difference. What the second form allows though is to “save” the block in a variable. Then the block can be called at some other point in time – callback. Ok. This time I went and … Read more

for x in y(): how does this work? [duplicate]

Using yield turns a function into a generator. A generator is a specialized type of iterator. for always loops over iterables, taking each element in turn and assigning it to the name(s) you listed. spinning_cursor() returns a generator, the code inside spinning_cursor() doesn’t actually run until you start iterating over the generator. Iterating over a … Read more

Return and yield in the same function

Yes, it’ still a generator. The return is (almost) equivalent to raising StopIteration. PEP 255 spells it out: Specification: Return A generator function can also contain return statements of the form: “return” Note that an expression_list is not allowed on return statements in the body of a generator (although, of course, they may appear in … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)