Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the yield function not require parentheses in Python?

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.

like image 855
ytpillai Avatar asked Jul 17 '15 03:07

ytpillai


4 Answers

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.

like image 132
NightShadeQueen Avatar answered Sep 21 '22 10:09

NightShadeQueen


print wasn't a function that gained parentheses: it went from being a statement to being a function. yield is still a statement, like return. Syntax highlighting is specific to your development environment.

You can find more information about the difference between expressions and statements here, and more about the difference between functions and statements here. Also see the documentation on simple statements and compound statements.

like image 43
TigerhawkT3 Avatar answered Sep 18 '22 10:09

TigerhawkT3


yield is not a function, its an keyword, and it does not require parenthesis according to its grammar -

yield_atom ::= "(" yield_expression ")"

yield_expression ::= "yield" [expression_list]

print used to be a statement in Python 2 , but it was changed to being a built-in function in Python 3 using PEP 3105

like image 23
Anand S Kumar Avatar answered Sep 18 '22 10:09

Anand S Kumar


print was a keyword defined by the language specification in Python 2, and became a builtin function (defined by the standard library specification) Python 3. yield was, and still is, a keyword.

like image 35
Jonathon Reinhart Avatar answered Sep 19 '22 10:09

Jonathon Reinhart