为什么yield函数在Python中不需要括号?
Why does the yield function not require parentheses in Python?
在Python中,我多次看到yield
函数用来创建生成器。这个和 print
函数在技术上都执行方法的操作,因为它们 return 一个值。但是,在从 Python 2 到 Python 3 的更改期间,print
函数像普通方法调用一样获得了括号,但 yield 保持不变。此外,yield
获得保留关键字的黄色,而 print
是保留方法的紫色。为什么 yield
不被视为一种方法并以这种方式着色而不使用括号语法?
(同理,为什么 return
也没有括号?)
让我再补充一些东西,yield 和 continue 在许多其他语言中也没有括号。我只是想知道是什么让它与众不同,而不是保留。还有许多其他的保留方法,它们有括号。
print
不是一个带有括号的函数:它从一个语句变成了一个函数。 yield
仍然是一个语句,就像 return
。语法突出显示特定于您的开发环境。
您可以找到有关 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 的更多信息。
yield
不是函数,它是关键字,根据 its grammar -
不需要括号
yield_atom ::= "(" yield_expression ")"
yield_expression ::= "yield" [expression_list]
print
曾经是 Python 2 中的语句,但在 Python 3 中使用 PEP 3105[=15 更改为内置函数=]
print
是由 语言规范 在 Python 2 中定义的 关键字 ,并成为 builtin 函数(由标准库规范定义)Python 3. yield
曾经是,现在仍然是 关键字 。
所以我去寻找答案。 And it turns out, there is one。来自 PEP 255,给我们 yield
关键字
的 pep
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.
在Python中,我多次看到yield
函数用来创建生成器。这个和 print
函数在技术上都执行方法的操作,因为它们 return 一个值。但是,在从 Python 2 到 Python 3 的更改期间,print
函数像普通方法调用一样获得了括号,但 yield 保持不变。此外,yield
获得保留关键字的黄色,而 print
是保留方法的紫色。为什么 yield
不被视为一种方法并以这种方式着色而不使用括号语法?
(同理,为什么 return
也没有括号?)
让我再补充一些东西,yield 和 continue 在许多其他语言中也没有括号。我只是想知道是什么让它与众不同,而不是保留。还有许多其他的保留方法,它们有括号。
print
不是一个带有括号的函数:它从一个语句变成了一个函数。 yield
仍然是一个语句,就像 return
。语法突出显示特定于您的开发环境。
您可以找到有关 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 的更多信息。
yield
不是函数,它是关键字,根据 its grammar -
yield_atom ::= "(" yield_expression ")"
yield_expression ::= "yield" [expression_list]
print
曾经是 Python 2 中的语句,但在 Python 3 中使用 PEP 3105[=15 更改为内置函数=]
print
是由 语言规范 在 Python 2 中定义的 关键字 ,并成为 builtin 函数(由标准库规范定义)Python 3. yield
曾经是,现在仍然是 关键字 。
所以我去寻找答案。 And it turns out, there is one。来自 PEP 255,给我们 yield
关键字
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.