Skip to content Skip to sidebar Skip to footer

Converting A Yield Statement To A Generator Expression In Python

I have a question regarding converting a yield statement to a generator expression So I have this small yield method that gets a function and a starting number as its inputs, and b

Solution 1:

Stick to what you have now. You could convert the function to a generator expression but it'd be an unreadable mess.

Generator expressions need another iterable to loop over, which your generator function doesn't have. Generator expressions also don't really have access to any other variables; there is just the expression and the loop, with optional if filters and more loops.

Here is what a generator expression would look like:

from itertools import repeat

number = [number]
gen = ((number[0], number.__setitem__(0, function(number[0]))[0] for _ inrepeat(True))

where we abuse number as a 'variable'.

Post a Comment for "Converting A Yield Statement To A Generator Expression In Python"