Why doesn't yield function require parentheses in Python?

In Python, I have seen the yield function used to create a generator many times. Both this function and the print function technically perform the action of the methods because they return a value. However, during the change from Python 2 to Python 3, the print function received brackets, such as a regular method call, but the output remained the same. In addition, yield receives the yellowish color of the reserved keyword, and print receives the purple color of the reserved method. Why is yield not considered by the method and painted in this way without using the syntax in parentheses?

(In the same vein, why return also lacks parentheses?)

Let me add something else, drop it and continue, also the brackets are not indicated in many other languages. I just wanted to know what makes him different. There are many other reserved methods that are enclosed in parentheses.

+7
python yield-keyword
source share
4 answers

So, I went in search of an answer. And it turns out there is one . From PEP 255, pep, which gave us the yield keyword

Q. Why is the new keyword for "yield"? Why is there no function instead?

but. The flow of control is much better expressed through the keyword in Python and the output is the control construct. He also believes that an efficient implementation in Jython requires the compiler to be able to identify potential pivot points at compile time, and the new keyword makes this easy. The implementation of links to CPython also exploits it to a large extent to determine which functions are generator functions (although a new keyword instead of β€œdef” would allow it for CPython - but people ask β€œwhy a new keyword?” The question does not need a new key word).

Q: Then why not some other special syntax without a new keyword? For example, one of them is 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: I missed one? Out of hundreds of messages, I counted three offering such an alternative, and I extracted the above from them. It would be nice not to use the new keyword, but it is better to make the crop very clear - I do not want to deduce that the profitability comes from the realization of a previously meaningless sequence of keywords or operators. Nevertheless, if this attracts sufficient interest, supporters should count on a single consensus proposal, and Guido will pronounce on it.

+15
source share

print not a function that reached the brackets: it turned from an expression into 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 in more detail about the difference between functions and statements here . Also see the documentation for simple instructions and composite reports .

+7
source share

yield not a function, its keyword and does not require brackets according to its grammar -

yield_atom :: = "(" yield_expression ")"

yield_expression :: = "yield" [list_expression]

print used as an operator in Python 2, but it was changed to a built-in function in Python 3 using PEP 3105

+2
source share

print was a keyword defined by the language specification in Python 2, and became a built-in function (defined by the standard library specification). Python 3. yield been and remains a keyword.

+1
source share

All Articles