Questions tagged [yield-from]

"yield from" is an expression in Python for delegating to a subgenerator.

The expression yield from was added in Python-3.3 based on PEP 380: Syntax for Delegating to a Subgenerator.

In the simplest case it can be used as a shorthand for iterating over a subgenerator, i.e.

for item in sth:
    yield item

can be replaced with:

yield from sth
59 questions
4
votes
1 answer

How can I apply iter() to a pagination api?

I watched Raymond Hettinger's Idiomatic Python talk, and learned about the sentinel argument to iter(). I'd like to try to apply it to a piece of code I'm working on iterating over an API that uses pagination (it's Twilio, but not relevant to my…
Sethish
  • 364
  • 2
  • 12
4
votes
1 answer

yield from a generator that has return statement in it

I have a generator with the return value statement in it. If i use next on it I get the Stopiteration: value from it as expected. However when I use yield from the value is lost. In [1]: def test(): ...: return 1 ...: yield 2 …
Mike D.
  • 43
  • 2
3
votes
3 answers

Avoiding extra `next` call after `yield from` in Python generator

Please see the below snippet, run with Python 3.10: from collections.abc import Generator DUMP_DATA = 5, 6, 7 class DumpData(Exception): """Exception used to indicate to yield from DUMP_DATA.""" def sample_gen() -> Generator[int | None, int,…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
3
votes
2 answers

How to desugar a yield from syntax in Python?

Now I'm studying on the differences between a yield-from and an await syntax. From the official python documentation, the yield-from generator() is just a syntax suger of the following code: for i in generator(): yield i But I can't desugar the…
BrainVader
  • 383
  • 1
  • 3
  • 7
3
votes
1 answer

Does CPython implement the mentioned optimizations from PEP 380?

PEP 380 mentions that the yield from expr syntax can be optimized in Python. PEP 380 - Optimizations Using a specialised syntax opens up possibilities for optimisation when there is a long chain of generators. Such chains can arise, for instance,…
Paebbels
  • 15,573
  • 13
  • 70
  • 139
3
votes
2 answers

Concurrent download and processing of large files in python

I have a list of URLs for large files to download (e.g. compressed archives), which I want to process (e.g. decompress the archives). Both download and processing take a long time and processing is heavy on disk IO, so I want to have just one of…
j08lue
  • 1,647
  • 2
  • 21
  • 37
2
votes
1 answer

Mixing yield and return. `yield [cand]; return` vs `return [[cand]]`. Why do they lead to different output?

Why does yield [cand] return lead to different output/behavior than return [[cand]] Minimal viable example uses recursion the output of the version using yield [1]; return is different than the output of the version using return [[1]] def…
joseville
  • 685
  • 3
  • 16
2
votes
2 answers

How to yield objects recursively using python generator?

I am writing a generator function which recursively walks through all the child nodes for a given astroid node. In the example below, node is an astroid functiondef node. node.getchildren() returns a generator with subnodes in the node. My goal is…
2
votes
2 answers

Unable to Yield From inside async function and get yielded data

I am trying to Yield From a function from within an asynchronous function. Having spent hours trying to figure this out and traversing Stack Overflow to find similar questions previously answered but unable to help me find a solution to my own…
2
votes
1 answer

Unsure of why the StopIteration isn't handled by "yield from"

Looking at the following sample code from the "Fluent Python" book that explains the "bidirectional tunnel" functionality of yield from, I have the following question. from collections import namedtuple Result = namedtuple('Result', 'count…
Noah Stebbins
  • 385
  • 1
  • 11
2
votes
4 answers

Breadth First Tree Traversal using Generators in Python

I am studying how to use Generators in Python in David Beazly's excellent Python Cookbook text. The following code recipe defines Depth First Tree Traversal using generators very elegantly: # example.py # # Example of depth-first search using a…
Bharat
  • 2,409
  • 6
  • 32
  • 57
2
votes
2 answers

What does the expression "a = yield from f()" mean?

So, here is a python expression: a = yield from f() What does it mean? Where can it be used? What kind of object should f be? What will be the value of a after the expression is evaluated? There are several questions here on stackoverflow about…
lesnik
  • 2,507
  • 2
  • 25
  • 24
2
votes
2 answers

Apply function for objects from "yield from" statement in python

# currently I have this code def some_func(): for match in re.finditer(regex, string): yield other_func(match) I was wondering if there was a way to squash it into one line # looking for something like def some_func(): yield from…
AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
1
vote
1 answer

How to understand `yield from` in python coroutine?

The code come form Fluent Python 1st edtion, I cannot understand the line while True: in grouper, delete that line raise a StopIteration error. But I find a new version of grouper without while True: that works. Why group.send(None) need another…
Animeta
  • 1,241
  • 3
  • 16
  • 30
1
vote
1 answer

How to save the output of recursive function to list of items using yield and generator functions

I have the following XML file from this link as sample: I have the following recursive function which prints output: import xml.etree.ElementTree as ET def perf_func(elem, func, level=0): func(elem,level) for child in elem.getchildren(): …