7

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.

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
rassa45
  • 3,482
  • 1
  • 29
  • 43

4 Answers4

15

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.

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
  • Thank you very much for this answer. I just thought that since everything has been logical in programming so far, there has got to be a logic to this one. – rassa45 Jul 17 '15 at 03:51
  • @ytpillai Then thank goodness you are starting with Python and not PHP (or ksh) - else you might lose some hair! – user2864740 Jul 17 '15 at 03:52
  • I've heard PHP described as being like a toolbox where everything is just a little... [off](https://c4.staticflickr.com/8/7226/7095238893_5000f6e57d_b.jpg). – TigerhawkT3 Jul 17 '15 at 03:55
  • `yield` would make no sense to me as a function. It doesn't behave like a function in any way. It's almost the exact opposite of a function, if you consider the call stack. – Jonathon Reinhart Jul 17 '15 at 03:55
8

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.

Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
2

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

Community
  • 1
  • 1
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Did people just decide for `yield` to not get parentheses or is there some sort of logic or origin behind this concept? – rassa45 Jul 17 '15 at 03:48
  • No there is a sort of origin behind `print` becoming a function, yield as such is not an application level functionality , but `print` is , most other languages as well have `print` as a function , so having `print` as a statement would require interpreter to specially treat it and they did not want to do that. – Anand S Kumar Jul 17 '15 at 03:53
  • Only function calls require parentheses. Please,go look at the python docs. – Jonathon Reinhart Jul 17 '15 at 03:53
1

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.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Even if it is builtin, there are many other builtin functions like `hash()` and `set()` and the universal `int()`Why do those get parentheses? – rassa45 Jul 17 '15 at 03:46
  • They are **functions**. – Jonathon Reinhart Jul 17 '15 at 03:49
  • How does something become a keyword and another thing a function? – rassa45 Jul 17 '15 at 03:49
  • I mean, they both act like a method that takes in data, processes, and gives back a value – rassa45 Jul 17 '15 at 03:49
  • In some way or the other – rassa45 Jul 17 '15 at 03:50
  • I'm not sure if you're trolling or just stubborn. Get more familiar with Python. Keywords are defined by the language. They're special tokens that you use to access features of the *language* itself (like create functions or classes or do control flow). Functions on the other hand are ***code*** that you are executing when you call them. – Jonathon Reinhart Jul 17 '15 at 03:52
  • Builtin functons are available without importing any libraries - theyre always available. – Jonathon Reinhart Jul 17 '15 at 03:53
  • `int()` and `float()` and `list()` are all available as a default function, but I understand what you mean now. I am not trolling or being stubborn; this would be a pretty horrible attempt at it anyways. I just never realized that a generator can be used for control flow, etc. Notice I did not ask about `for` and looping and `if` statements – rassa45 Jul 17 '15 at 03:55
  • 1
    The `yield` statement does NOT take in data, does NOT process data, and does NOT return a value. You have completely mischaracterized what `yield` does. What it actually does is, trigger a control flow that causes the function that contains it to return a value (in a specific way that's distinct from non-generator functions). Why are you so mystified that `yield` isn't a function? It manifestly behaves in a manner entirely unlike a function. – Chris Johnson Jul 17 '15 at 04:16