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.

11
  • 6
    Its not a function
    – user1971598
    Jul 17, 2015 at 3:42
  • 4
    points at the lack of parens for return Jul 17, 2015 at 3:44
  • 4
    Because that's the way the language is specified. You may just as well ask why the keywords are in English rather than Esperanto :-) I'm having difficulty figuring out how an answer will actually be useful to anyone other than as a curiosity.
    – paxdiablo
    Jul 17, 2015 at 3:50
  • 3
    Your answer is in PEP 255. In the future, if you want to know why something is some way in python, check out the relevant PEP. Jul 17, 2015 at 3:51
  • 1
    Seriously, you differentiate between keywords and functions by their colours?
    – xyres
    Sep 2, 2015 at 17:32

4 Answers 4

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.

4
  • 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, 2015 at 3:51
  • @ytpillai Then thank goodness you are starting with Python and not PHP (or ksh) - else you might lose some hair! Jul 17, 2015 at 3:52
  • I've heard PHP described as being like a toolbox where everything is just a little... off. Jul 17, 2015 at 3: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. Jul 17, 2015 at 3: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.

2
  • Why are these considered statements?
    – rassa45
    Jul 17, 2015 at 3:44
  • Why not the others, and just these?
    – rassa45
    Jul 17, 2015 at 3:48
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

3
  • 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, 2015 at 3: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. Jul 17, 2015 at 3:53
  • Only function calls require parentheses. Please,go look at the python docs. Jul 17, 2015 at 3: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.

9
  • 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, 2015 at 3:46
  • They are functions. Jul 17, 2015 at 3:49
  • How does something become a keyword and another thing a function?
    – rassa45
    Jul 17, 2015 at 3:49
  • I mean, they both act like a method that takes in data, processes, and gives back a value
    – rassa45
    Jul 17, 2015 at 3:49
  • 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. Jul 17, 2015 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.