Skip to content
Advertisement

Can you yield from a lambda function?

I have a generator function in a class:

JavaScript

In another function I initalize it as a variable:

JavaScript

And it’s yielded as necessary:

JavaScript

Can defining the generator be done in one line, however? I’ve considered the below:

JavaScript

Here’s a minimal code I’m working with:

JavaScript

Output is below:

JavaScript

I just wanted to see if I could get rid of Foo.generator and instead declare the generator in one line.

Advertisement

Answer

You are returning a list comprehension. You can just do:

JavaScript

that will generate a generator with the values you want. You test it later with next(var) is in your code.

User contributions licensed under: CC BY-SA
11 People found this is helpful
Advertisement