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
1
vote
1 answer

"yield" and "yield from" at same function

I need use yield and yield from at same function, but it seems not works as intended, once that it yields only the last yield from or yield (what comes last). My code is (https://3v4l.org/jFDXh): function yieldItems() { yield 1; yield 2; …
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
1
vote
1 answer

How to reset yield from within a generator in PHP

I am trying to setting up a coupled yield function. But when the function is called the second time, the integrated yield from is not reset. I think the simplest way is to show using an example: Code Example PHP class GeneratorTest { public…
LeTigre
  • 440
  • 4
  • 16
1
vote
0 answers

Why 'yield from' needs an enclosing 'while 1'?

I'm trying to understand how 'yield from' is used. For that I've composed a simple (rather useless) example (count_vowels.py) below which when run produces: $ python3 count_vowels.py Counter({'a': 5}) Please explain why 'while True' is needed in…
1
vote
1 answer

In Python, yielding from lock in DatagramProtocol.datagram_received makes the function never called

I want to synchronize data from between coroutines and I end up with a method not being called whenever there is "yield" inside it. To be more precise, when I implement a DatagramProtocol class with the method datagram_received as per the doc…
1
vote
2 answers

'yield from' substitute in Python 2

My code uses yield from in python3 in recursive calls and it works perfectly fine. The problem right now is that this was introduced from PEP-380 in python 3.3 and I need it to work in python 2.7. I read up on a few articles and none of them were…
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
1
vote
0 answers

Yield from in listcomp or genexpr difference

Why the result is different? In [19]: [(yield from (1, 2)) for x in [1]] Out[19]: at 0x0438B3C0> In [20]: list(_) Out[20]: [1, 2] In [21]: ((yield from (1, 2)) for x in [1]) Out[21]: at…
1
vote
1 answer

Flexible XML to dictionary using Python3

I'm being provided an XML file containing the data I need and I need to convert it to a CSV. This should be straightforward, but the number of children for the "repeating unit" of the XML is not always the same. What I am trying to work out is how…
1
vote
0 answers

What's the difference between 'yield' and 'yield from' in Python?

I'm using the basic yield keyword a lot in my code to build generators and coroutines... While searching for various yield related solutions on the web, I also found the yield from syntax. Both are used in generators. I don't feel that I'm missing a…
Paebbels
  • 15,573
  • 13
  • 70
  • 139
1
vote
1 answer

python using yield from in a function

i have a list like: list=['2,130.00','2,140.00','2,150.00','2,160.00'] i would like to use a function like def f(iterable): yield from iterable and applying float(item.replace(',','')) for item in iterable at the same time so that…
0
votes
0 answers

yield inhibits new web pages from display

I've successfully scrapped a multi page library using “scrapy”. But when I added a yield block only the first web page was scrapped. The page count continued to increase but the web page didn't change What is wrong? Here is the structure of part…
mafox
  • 61
  • 4
0
votes
0 answers

Chaining activities on azure durable functions using yield from

I am using a azure durable-function to orchestrate a scraper of mine. I want to scrape the web in batches of 100 ages at a time in each activity, and have set up the following orchestration: import ujson def orchestrator_function(context:…
0
votes
1 answer

In Python is faster using "yield from" than yield in a loop?

I tried multiple ranges for this example I let above: import time def reader(): for a in range(100000000): yield a def reader_wrapper(gen): for i in gen: yield i def reader_wrapper_enhanced(gen): yield from…
Hikager
  • 23
  • 1
  • 6
0
votes
1 answer

Trigger except clause in generator from exception raised in user code

I have a series of nested generators, and I would like to know from the first generator if an exception ocurred in the user code, for the sake of an example, consider the code…
Mite
  • 57
  • 1
  • 9
0
votes
2 answers

extracting the actual output of generator in a list

I am trying to get the actual output from the generator but i get the ouput as generator object. Please help me achieve the actual output from the generator import spacy nlp = spacy.load('en') def lemmatizer(words): yield from [w.lemma_ for w…
prog
  • 1,073
  • 5
  • 17
0
votes
1 answer

Using yield won't generate new numbers (Using next function)

I am trying to use yield to generate new numbers on each iteration as shown below: def nextSquare(): i = 1 # An Infinite loop to generate squares while True: yield i*i i += 1 # Next execution resumes…
Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62