In Python, I have many times seen the yield
function used to create a generator. Both this and the print
function technically both perform the action of methods because they return a value. However, during the change from Python 2 to Python 3, the print
function gained parentheses like a normal method call, but yield stayed the same. Also, yield
gains a yellowish color of a reserved keyword while print
is the purple of a reserved method. Why is yield
not considered a method and colored this way along with not using parentheses syntax?
(In a similar vein, why does return
also lack parentheses?)
Let me add some more stuff, yield and continue are not given parentheses in many other languages as well. I just wanted to know what makes it different other than it is reserved. There are many other reserved methods out there which get parentheses.
So I went digging for an answer. And it turns out, there is one. From PEP 255, the pep that gave us the yield
keyword
Q. Why a new keyword for "yield"? Why not a builtin function instead?
A. Control flow is much better expressed via keyword in Python, and yield is a control construct. It's also believed that efficient implementation in Jython requires that the compiler be able to determine potential suspension points at compile-time, and a new keyword makes that easy. The CPython referrence implementation also exploits it heavily, to detect which functions are generator- functions (although a new keyword in place of "def" would solve that for CPython -- but people asking the "why a new keyword?" question don't want any new keyword).
Q: Then why not some other special syntax without a new keyword? For example, one of these instead of "yield 3":
return 3 and continue
return and continue 3
return generating 3
continue return 3
return >> , 3
from generator return 3
return >> 3
return << 3
>> 3
<< 3
* 3
A: Did I miss one ? Out of hundreds of messages, I counted three suggesting such an alternative, and extracted the above from them. It would be nice not to need a new keyword, but nicer to make yield very clear -- I don't want to have to deduce that a yield is occurring from making sense of a previously senseless sequence of keywords or operators. Still, if this attracts enough interest, proponents should settle on a single consensus suggestion, and Guido will Pronounce on it.