yield

What does a yield inside a yield do?

What does a yield inside a yield do? Question: Consider the following code: def mygen(): yield (yield 1) a = mygen() print(next(a)) print(next(a)) The output yields: 1 None What does the interpreter do at the “outside” yield exactly? Asked By: David || Source Answers: Any generator exhausts elements till it runs out of them. In …

Total answers: 4

How to idiomatically open multiple managed resources from an object method in Python

How to idiomatically open multiple managed resources from an object method in Python Question: What is the most Pythonic way of constructing an object to open multiple (context managed) resources and do work with those resources? I have a class which opens several managed resources, which are then operated on in class methods. For example, …

Total answers: 1

Python: Weird behavior while using `yield from`

Python: Weird behavior while using `yield from` Question: In the following code, I have run into a RecursionError: maximum recursion depth exceeded. def unpack(given): for i in given: if hasattr(i, ‘__iter__’): yield from unpack(i) else: yield i some_list = [‘a’, [‘b’, ‘c’], ‘d’] unpacked = list(unpack(some_list)) This works fine if I use some_list = [1, …

Total answers: 1

What's the advantage of using yield in __iter__()?

What's the advantage of using yield in __iter__()? Question: What is the advantage of using an generator(yield) inside an __iter__() function? After reading through Python Cookbook I understand “If you want a generator to expose extra state to the user, don’t forget that you can easily implement it as a class, putting the generator function …

Total answers: 1

What is the return type hint of a generator function?

What is the return type hint of a generator function? Question: I’m trying to write a :rtype: type hint for a generator function. What is the type it returns? For example, say I have this functions which yields strings: def read_text_file(fn): “”” Yields the lines of the text file one by one. :param fn: Path …

Total answers: 3

How can I tell whether a generator was just-started?

How can I tell whether a generator was just-started? Question: I’d like a function, is_just_started, which behaves like the following: >>> def gen(): yield 0; yield 1 >>> a = gen() >>> is_just_started(a) True >>> next(a) 0 >>> is_just_started(a) False >>> next(a) 1 >>> is_just_started(a) False >>> next(a) Traceback (most recent call last): File “<stdin>”, …

Total answers: 3

Python — Iterate an iterator twice

Python — Iterate an iterator twice Question: Edit: There is a similar question here that deals with iterator resetting. The accepted answer below however addresses the actual issue of nested iterators, and handles an easy to miss issue, whereby the nested iterator doesn’t reset. Is there any way to iterate over an iterator twice in …

Total answers: 4

How to use 'yield' inside async function?

How to use 'yield' inside async function? Question: I want to use generator yield and async functions. I read this topic, and wrote next code: import asyncio async def createGenerator(): mylist = range(3) for i in mylist: await asyncio.sleep(1) yield i*i async def start(): mygenerator = await createGenerator() for i in mygenerator: print(i) loop = …

Total answers: 3

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

Apply function for objects from "yield from" statement in python Question: # 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 other_func(re.finditer(regex, string)) Asked By: AlanSTACK || …

Total answers: 2

what's the difference between yield from and yield in python 3.3.2+

what's the difference between yield from and yield in python 3.3.2+ Question: After python 3.3.2+ python support a new syntax for create generator function yield from <expression> I have made a quick try for this by >>> def g(): … yield from [1,2,3,4] … >>> for i in g(): … print(i) … 1 2 3 …

Total answers: 4