1

I encountered this term frequently that this function yields somethings or this function return something. I'm trying to understand this and read few articles in python. Then I encountered same statement in c++ which says:

some expressions yield objects but return them as rvalues, not lvalues.

Can anyone help in understanding these two terms in language independent way or in detailed manner so I can grasp it easily.

Edit - if they are different in both languages please explain in both or any one which you know.

martineau
  • 119,623
  • 25
  • 170
  • 301
Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
  • I am not a C++ programmer but I think they are not the same concept in Python and C++ – juanpa.arrivillaga Oct 22 '19 at 00:04
  • Programming languages are not English. Sometimes they use words differently than their natural definitions. – Barmar Oct 22 '19 at 00:04
  • 2
    In English "yield" and "return" are synonyms in this context, but in Python `return` is used for ordinary functions, while `yield` is used for generators. – Barmar Oct 22 '19 at 00:05
  • 1
    Does C++ have a `yield`? I didn't think it did. [This question](https://stackoverflow.com/q/7213839/10957435) doesn't seem to indicate it does either. The only `yield` I can find is one about threads which I don't think does the same thing as in Python. –  Oct 22 '19 at 00:07
  • C++ doesn't have a `yield` keyword. Without knowing the context of the quote, I bet they were trying to distinguish expressions from functions. – Otto von Bisquick Oct 22 '19 at 00:09
  • @OttovonBisquick In C++, an lvalue expression yields an object or a function. However, some lvalues, such as const objects, may not be the left- hand operand of an assignment. Moreover, some expressions yield objects but return them as rvalues, not lvalues. – Nikhil Badyal Oct 22 '19 at 00:11
  • @OttovonBisquick this was the whole statement written over there – Nikhil Badyal Oct 22 '19 at 00:11
  • I believe my original assessment to be correct - they are trying to not make you confused between the keyword `return` and the evaluation of an expression. – Otto von Bisquick Oct 22 '19 at 00:13
  • 1
    I think that C++ 20 is going to introduce the `co_yield` keyword to implement coroutines. – jkb Oct 22 '19 at 00:13
  • 1
    @Chipster The `co_yield` keyword is new with [C++20 coroutines](https://en.cppreference.com/w/cpp/language/coroutines). It's not literally spelled "yield" but the committee has a difficult time adding new keywords without breaking everyone's code. – Blastfurnace Oct 22 '19 at 00:18
  • Just wait until you hit the differences between a C++ object and an OOP object. Much fun. – user4581301 Oct 22 '19 at 00:22
  • Welcome to SO! When you place a question try to add a minimum content: input sample, expected output sample, what did you try, research and where are you stacked. What about your research? Check [this](https://stackoverflow.com/questions/2037267/different-results-from-yield-vs-return), [this](https://stackoverflow.com/questions/45695872/yield-return-vs-return-ienumerablet), [this](https://stackoverflow.com/questions/30187598/yield-from-iterable-vs-return-iteriterable) or [this](https://stackoverflow.com/questions/58494900/using-return-list-vs-yield) – David García Bodego Oct 22 '19 at 04:21
  • Possible duplicate of [What does the "yield" keyword do?](https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do) – Alessandro Da Rugna Oct 22 '19 at 05:59
  • Personally, I don't think the word "yield" in the quoted text (from some C++ documentation?) has anything to do with a `yield` statement in Python. It simply means "[produces](https://www.dictionary.com/browse/yield)". – martineau Oct 22 '19 at 17:28

2 Answers2

1

In Python, yield is used for generation. For example:

def func():
    i =0
    while True:
        i += 1
        yield i

If I remember Python correctly, this should allow this function to basically pause execution and get called over and over again. This can generate some sequence like {0,1,2,3...}.

On the other hand, return just returns a single value and ends execution:

def func():
    i =0
    while True:
        i += 1
        return i

This should always return 0, since the function ends execution completely so i goes out of scope every time.


On the other hand, C++ has no direct real equivalent to yield as far as I'm aware (except for apparently in the new C++20, which is adding an equivalent), where as it does have an equivalent (in all versions) to return here. It is, of course, called return.

That said, C++ can achieve something similar to our yield example using static variables:

int func() {
    static i = 0;
    return i++;
}

However, that is not to say that static variables are replacements for yield in C++. It's just that you can sort of achieve the same thing in C++ with static variables in this (and possibly other) example(s).


So, in short, return ends execution of a function in both languages whereas yield allows a function to sort of resume execution. There is no real equivalent for Python's yield in C++ until at least C++20.

  • @martineau thanks. It there anything else I missed? It's like I say, my Python is rusty. –  Oct 22 '19 at 01:16
  • Thanks. I added the link to the answer--and read it too, of course. :D –  Oct 22 '19 at 01:23
  • Is that better, or do I still need the global? –  Oct 22 '19 at 15:48
  • No, using a global isn't a requirement (and using a local like that is better). Again, I apologize, and will delete my earlier comments. Note there's no reason for a loop in the version using `return`. – martineau Oct 22 '19 at 17:09
  • It's all good. It's not your job to make sure my Python is correct. I sure appreciate it. –  Oct 22 '19 at 17:19
  • Side note: the loop in `return` is just to illustrate a point. –  Oct 22 '19 at 17:20
0

Have you ever tried to iterate over an entire database of objects? That's what I tried my first time, and it quickly consumed all 16GB of my memory and ground my system to a halt. This is why generators exist - to load data in as needed instead of all at once (and probably a few other uses as well). Try reading this post, it has a few examples and will go into more detail.

nightsh4de
  • 26
  • 2