yield

Avoid Multiple Next () Statement in Python Generator

Avoid Multiple Next () Statement in Python Generator Question: I’m using a library that returns a generator. Is there a way to start at a particular iteration without using multiple next () statement? In a simple for loop, I could do the following. array = [2, 5, 1, 4, 3] for i in array [2:]: …

Total answers: 2

yield in list comprehensions and generator expressions

yield in list comprehensions and generator expressions Question: The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] <generator object <listcomp> at 0x0245C148> >>> list([(yield i) for i in range(3)]) [0, 1, 2] >>> list((yield i) for i in range(3)) [0, None, 1, None, 2, None] The intermediate …

Total answers: 1

Python: yield and yield assignment

Python: yield and yield assignment Question: How does this code, involving assignment and the yield operator, work? The results are rather confounding. def test1(x): for i in x: _ = yield i yield _ def test2(x): for i in x: _ = yield i r1 = test1([1,2,3]) r2 = test2([1,2,3]) print list(r1) print list(r2) Output: …

Total answers: 3

Is there any shorthand for 'yield all the output from a generator'?

Is there any shorthand for 'yield all the output from a generator'? Question: Is there a one-line expression for: for thing in generator: yield thing I tried yield generator to no avail. Asked By: Walrus the Cat || Source Answers: In Python 3.3+, you can use yield from. For example, >>> def get_squares(): … yield …

Total answers: 3

how to write generate Fibonacci in python

how to write generate Fibonacci in python Question: def fib(a, b, f): fib must generate (using yield) the generalized Fibonacci sequence, a and b is first and second element. f is function to get the third element instead of a+b as normal Fibonacci sequence. Use take function(which show below) to test it. my code is …

Total answers: 4

decorating a function that yields

decorating a function that yields Question: Is it possible, and if so, advisable, and if so, what would be the recommended method for decorating a function that yields a value? For example, consider this imaginary example I made up def foobar_creator(func): def wrapped(**kwargs): res = func(**kwargs) flag = True for k,v in kwargs: if res …

Total answers: 5

Return and yield in the same function

Return and yield in the same function Question: What exactly happens, when yield and return are used in the same function in Python, like this? def find_all(a_str, sub): start = 0 while True: start = a_str.find(sub, start) if start == -1: return yield start start += len(sub) # use start += 1 to find overlapping …

Total answers: 4

Issue with a python function returning a generator or a normal object

Issue with a python function returning a generator or a normal object Question: I defined the function f as def f(flag): n = 10 if flag: for i in range(n): yield i else: return range(n) But f returns a generator no matter what flag is: >>> f(True) <generator object f at 0x0000000003C5EEA0> >>> f(False) <generator …

Total answers: 3

Converting "yield from" statement to Python 2.7 code

Converting "yield from" statement to Python 2.7 code Question: I had a code below in Python 3.2 and I wanted to run it in Python 2.7. I did convert it (have put the code of missing_elements in both versions) but I am not sure if that is the most efficient way to do it. Basically …

Total answers: 7

Python – Implement iteration over certain class attributes on given order

Python – Implement iteration over certain class attributes on given order Question: I have a Position class, and it has two attributes, Lat and Lon. I would like the following API by implementing iterator protocol (but some googling just confused me more): pos = Position(30, 50) print pos.Latitude > 30 print pos.Longitude > 50 for …

Total answers: 1