yield

Return several values inside for loop

Return several values inside for loop Question: I have a function that must return several values using a for loop. I do not wish to store the values inside a list or a dict. Because of the use of the return, I only get the first value. How can I return all values successively? I …

Total answers: 2

yield from vs yield in for-loop

yield from vs yield in for-loop Question: My understanding of yield from is that it is similar to yielding every item from an iterable. Yet, I observe the different behavior in the following example. I have Class1 class Class1: def __init__(self, gen): self.gen = gen def __iter__(self): for el in self.gen: yield el and Class2 …

Total answers: 2

how to connect three dataloaders together in pytorch – parallel not chained

how to connect three dataloaders together in pytorch – parallel not chained Question: i have three same length dataloaders (A,B,C) that load images (a,b,c), i want to create a new dataloader D that loads a dict of images, some syntax for clarity: usually the dataloader works like this: for a in A: a -> an …

Total answers: 1

Python generator yield behaviour

Python generator yield behaviour Question: So I have the following generator function: def gen(n=5): for i in range(n): n = yield n for i in gen(3): print(i) The result: 3 None None I understand the first result of yield is 3. Because I assigned 3 to function argument n. But where are the None in …

Total answers: 2

yield + generator in python in class

yield + generator in python in class Question: I am very new in Python and I wanna create a generator object that yields two lists for Fibonacci sequence. First list of number and second list of fibonacci. The function define in the class. Before it I define fibonacci function as below: def fib(self, _n,) -> …

Total answers: 1

How to get two synchronised generators from a function

How to get two synchronised generators from a function Question: i have a nested tuple like this one : this_one = (w,h,(1,2,4,8,16),(0,2,3,4,6),("0"),("0"),(0,1)) It will be used to feed: itertools.product(*this_one) w and h have to be both generators. Generated values from h depends on generated values from w as in this function: def This_Function(maxvalue): for i …

Total answers: 2

convert normal function to generator and keeping code structure

convert normal function to generator and keeping code structure Question: Generators are elegant and memory-efficient, so I’d like to convert normal functions producing sequences to generators. But I haven’t found an easy conversion method, by easy I mean it is best to be done automatically by some tools, maybe simple as string or regex replacement(return …

Total answers: 1

Generating permutations in Python 3

Generating permutations in Python 3 Question: I wrote this simple permutations generator in Python 3. The permuted digits are a string, and the function should yield successive strings as well. The function returns nothing. Could I please ask for some help with this? def permutations(digits): if digits: for d in digits: remaining = digits.replace(d,"") for …

Total answers: 3

python gRPC client disconnect while server streaming response

python gRPC client disconnect while server streaming response Question: Good day, This is my first time posting so forgive me if I do something wrong with the post. I am trying to get a subscription type service running, which works fine up until the client disconnects. Depending on the timing, this works fine or blocks …

Total answers: 2