Why does the yield function not require parentheses in Python?
Asked Answered
D

4

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.

Depreciate answered 17/7, 2015 at 3:41 Comment(10)
Its not a functionHorrendous
It gives back a value, howeverDepreciate
Why someone down voted this question? It's look ok for me.Pucker
points at the lack of parens for returnElurd
Dang forgot about that too thanks NightShadeQueenDepreciate
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.Gladis
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.Elurd
@Gladis Yes, and I kind of realize how generators can control flow in codeDepreciate
Seriously, you differentiate between keywords and functions by their colours?Accentor
related softwareengineering.stackexchange.com/questions/40981/…Garrick
E
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.

Elurd answered 17/7, 2015 at 3:41 Comment(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.Depreciate
@ytpillai Then thank goodness you are starting with Python and not PHP (or ksh) - else you might lose some hair!Charleen
I've heard PHP described as being like a toolbox where everything is just a little... off.Johniejohnna
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.Staminody
J
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.

Johniejohnna answered 17/7, 2015 at 3:43 Comment(2)
Why are these considered statements?Depreciate
Why not the others, and just these?Depreciate
G
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

Gavage answered 17/7, 2015 at 3:44 Comment(3)
Did people just decide for yield to not get parentheses or is there some sort of logic or origin behind this concept?Depreciate
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.Gavage
Only function calls require parentheses. Please,go look at the python docs.Staminody
S
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.

Staminody answered 17/7, 2015 at 3:44 Comment(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?Depreciate
They are functions.Staminody
How does something become a keyword and another thing a function?Depreciate
I mean, they both act like a method that takes in data, processes, and gives back a valueDepreciate
In some way or the otherDepreciate
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.Staminody
Builtin functons are available without importing any libraries - theyre always available.Staminody
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 statementsDepreciate
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.Beneath

© 2022 - 2024 — McMap. All rights reserved.