Questions tagged [yield]

yield is (1) a keyword that facilitates creation of generator functions, (2) a Ruby statement to transfer control from one coroutine to another, (3) a Java statement used to yield a value from a switch expression.

In , the yield statement is only used when defining a generator function, and is only used in the body of the generator function. Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function.

In , the yield statement is in context of coroutines generally used to transfer control from one coroutine to another, for example from a method to a block passed into it as an argument.

’s yield return is equivalent to Python’s yield, and yield break is just return in Python. In C#, yield is used in an iterator block to provide a value to the enumerator object or to signal the end of iteration.

yield is used in and generator functions in the same way it is in Python generator functions.

In yield is used in for-comprehension construction. for-comprehension iterates over one or more collections and uses yield to create and return new collection.

In , yield is a keyword used in a statement yield: <yield-target> to yield a value, which becomes the value of the enclosing switch expression.

1730 questions
74
votes
4 answers

yield break in Python

According to answer to this question, yield break in C# is equivalent to return in Python. In the normal case, return indeed stops a generator. But if your function does nothing but return, you will get a None not an empty iterator, which is…
Sonic Lee
  • 1,411
  • 2
  • 14
  • 15
71
votes
1 answer

What is the result of a yield expression in Python?

I know that yield turns a function into a generator, but what is the return value of the yield expression itself? For example: def whizbang(): for i in range(10): x = yield i What is the value of variable x as this function…
slacy
  • 11,397
  • 8
  • 56
  • 61
68
votes
13 answers

check if function is a generator

I played with generators in Nodejs v0.11.2 and I'm wondering how I can check that argument to my function is generator function. I found this way typeof f === 'function' && Object.getPrototypeOf(f) !== Object.getPrototypeOf(Function) but I'm not…
Dima Vidmich
  • 1,335
  • 1
  • 11
  • 9
64
votes
4 answers

Rails 3: yield/content_for with some default value?

Is there any way to detect if #content_for was actually applied to a yield scope in Rails? A classic example being something like: <%= yield :page_title %> If a template doesn't set that with <% content_for :page_title, "Something…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
62
votes
3 answers

How to Pythonically yield all values from a list?

Suppose I have a list that I wish not to return but to yield values from. What is the most pythonic way to do that? Here is what I mean. Thanks to some non-lazy computation I have computed the list ['a', 'b', 'c', 'd'], but my code through the…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
59
votes
4 answers

What does a yield inside a yield do?

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?
David
  • 2,926
  • 1
  • 27
  • 61
57
votes
3 answers

C#: yield return range/collection

I use the yield return keyword quite a bit, but I find it lacking when I want to add a range to the IEnumerable. Here's a quick example of what I would like to do: IEnumerable SomeRecursiveMethod() { // some code // ... yield…
André Haupt
  • 3,294
  • 5
  • 32
  • 57
56
votes
4 answers

How does a threading.Thread yield the rest of its quantum in Python?

I've got a thread that's polling a piece of hardware. while not hardware_is_ready(): pass process_data_from_hardware() But there are other threads (and processes!) that might have things to do. If so, I don't want to burn up cpu checking the…
Tom Future
  • 1,900
  • 2
  • 15
  • 15
53
votes
9 answers

Yield in a recursive function

I am trying to do something to all the files under a given path. I don't want to collect all the file names beforehand then do something with them, so I tried this: import os import stat def explore(p): s = '' list = os.listdir(p) for a in…
Ali
  • 18,665
  • 21
  • 103
  • 138
52
votes
2 answers

what does yield without value do in context manager

import contextlib import time @contextlib.contextmanager def time_print(task_name): t = time.time() try: yield finally: print task_name, "took", time.time() - t, "seconds." def doproc(): x=1+1 with…
Shuman
  • 3,914
  • 8
  • 42
  • 65
50
votes
4 answers

Ruby's yield feature in relation to computer science

I recently discovered Ruby's blocks and yielding features, and I was wondering: where does this fit in terms of computer science theory? Is it a functional programming technique, or something more specific?
hbw
  • 15,560
  • 6
  • 51
  • 58
50
votes
3 answers

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

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 >>>…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
49
votes
4 answers

Where to use yield in Python best?

I know how yield works. I know permutation, think it just as a math simplicity. But what's yield's true force? When should I use it? A simple and good example is better.
whi
  • 2,685
  • 6
  • 33
  • 40
48
votes
1 answer

Passing multiple code blocks as arguments in Ruby

I have a method which takes a code block. def opportunity @opportunities += 1 if yield @performances +=1 end end and I call it like this: opportunity { @some_array.empty? } But how do I pass it more than one code block so that I could use…
doctororange
  • 11,670
  • 12
  • 42
  • 58
48
votes
4 answers

C#: yield return within a foreach fails - body cannot be an iterator block

Consider this bit of obfuscated code. The intention is to create a new object on the fly via the anonymous constructor and yield return it. The goal is to avoid having to maintain a local collection just to simply return it. public static…
p.campbell
  • 98,673
  • 67
  • 256
  • 322