在问题中“yield”关键字有什么作用? https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do,我发现正在使用一种我没想到有效的 Python 语法。这个问题很老了,而且有很多票,所以我很惊讶没有人至少对这个函数定义发表评论:
def node._get_child_candidates(self, distance, min_dist, max_dist):
if self._leftchild and distance - max_dist < self._median:
yield self._leftchild
if self._rightchild and distance + max_dist >= self._median:
yield self._rightchild
我试图评估这种语法:
到目前为止失败了
语法错误:语法无效
我查了一下链接(可能已过时) http://well-adjusted.de/~jrschulz/mspace/问题中给出,并在网上搜索了def
,但我没有发现任何解释这种“点名”模式的内容。我正在使用Python 3,也许这是Python 2的一个特性?
该语法是否(或曾经)有效,如果是,则意味着什么?
不,语法无效。通过查看文档很容易证明。在Python 2中,标识符由以下内容构造rules https://docs.python.org/2/reference/lexical_analysis.html#identifiers:
identifier ::= (letter|"_") (letter | digit | "_")*
letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"
在 Py3 中,除了扩展到 Unicode 字符范围之外,规则或多或少是相同的。
看来作者的意思大概是这样的
class Node:
...
def _get_child_candidates(self, ...):
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)